bezierVertex function drawing problem needs help
in
Programming Questions
•
4 months ago
I am working on a sketch to draw a filled vector shape and animate smoothly its three vertices. I include the code below. The sketch is interactive. If you run it, you will see the triangular vector shape--it looks like a guitar pick.
Here is the sketch:
15" MacBook Pro, 2.4GHz Intel Core i5, OS X v.10.8.3 Mountain Lion,
QuickTime Player v.10.2, Processing v. 1.5.1 and v.2.0b8, Arduino Uno, Nano v.3.
If you press the "a" key on the keyboard, the three vertices animate. When this is animated, the first and last vertex do not meet perfectly. There is a sharp point there instead of a smooth curve. I can't figure out why.
Here is a diagram of the vertices and control points. The three vertices are (curX, curY), (curX1, curY1) and (curX2, curY2). The values at the corners of the largest triangle are the control points. (curX, curY) is the first and last vertex specified. It is the vertex where the glitch occurs.
Here is the sketch:
boolean interactive;
int curX, curY, tarX, tarY, signX, signY;
int curX1, curY1, tarX1, tarY1, signX1, signY1;
int curX2, curY2, tarX2, tarY2, signX2, signY2;
int x, y;
void setup() {
size(500, 500);
interactive = false;
x = 250;
y = 250;
setInitialValues();
}
void draw() {
background(200);
fill(255, 255, 0);
if (interactive) { //==============
beginShape();
//vertex(x, y-38);
vertex(curX, curY);
bezierVertex(curX+87, curY, curX1+44, curY1-76, curX1, curY1);
bezierVertex(curX1-43, curY1+75, curX2+43, curY2+75, curX2, curY2);
bezierVertex(curX2-44, curY2-76, curX-87, curY-38, curX, curY);
endShape(); // ==================
}
else {
beginShape();
vertex(x, y-38);
bezierVertex(x+87, y-38, x+87, y-38, x+43, y+38);
bezierVertex(x, y+113, x, y+113, x-43, y+38);
bezierVertex(x-87, y-38, x-87, y-38, x, y-38);
endShape();
}
updateLocation();
//println(curX2 + ", " + tarX2);
}
void keyPressed() {
if (key == 'a') {
interactive = ! interactive;
}
}
void setInitialValues() {
// top vertex =======================================
curX = int (random(width));
tarX = int (random(width));
while (curX == tarX) {
tarX = int (random(width));
}
if (curX > tarX) {
signX = -1;
}
else
{
signX = 1;
}
curY = int (random(width));
tarY = int (random(width));
while (curY == tarY) {
tarY = int (random(width));
}
if (curY > tarY) {
signY = -1;
}
else
{
signY = 1;
}
// LR vertex ========================================
curX1 = int (random(width));
tarX1 = int (random(width));
while (curX1 == tarX1) {
tarX1 = int (random(width));
}
if (curX1 > tarX1) {
signX1 = -1;
}
else
{
signX1 = 1;
}
curY1 = int (random(width));
tarY1 = int (random(width));
while (curY1 == tarY1) {
tarY1 = int (random(width));
}
if (curY1 > tarY1) {
signY1 = -1;
}
else
{
signY1 = 1;
}
// LL vertex=========================================
curX2 = int (random(width));
tarX2 = int (random(width));
while (curX2 == tarX2) {
tarX2 = int (random(width));
}
if (curX2 > tarX2) {
signX2 = -1;
}
else
{
signX2 = 1;
}
curY2 = int (random(width));
tarY2 = int (random(width));
while (curY2 == tarY2) {
tarY2 = int (random(width));
}
if (curY2 > tarY2) {
signY2 = -1;
}
else
{
signY2 = 1;
}
}
void updateLocation() {
// top vertex ========================
if (curX == tarX) {
getNewTargetX();
}
else {
curX = curX + 1 * signX;
}
if (curY == tarY) {
getNewTargetY();
}
else {
curY = curY + 1 * signY;
}
// LR vertex =========================
if (curX1 == tarX1) {
getNewTargetX1();
}
else {
curX1 = curX1 + 1 * signX1;
}
if (curY1 == tarY1) {
getNewTargetY1();
}
else {
curY1 = curY1 + 1 * signY1;
}
// LL vertex =========================
if (curX2 == tarX2) {
getNewTargetX2();
}
else {
curX2 = curX2 + 1 * signX2;
}
if (curY2 == tarY2) {
getNewTargetY2();
}
else {
curY2 = curY2 + 1 * signY2;
}
}
// top vertex items =======================
void getNewTargetX() {
tarX = int (random(width));
while (curX == tarX) {
tarX = int (random(width));
}
if (curX > tarX) {
signX = -1;
}
else
{
signX = 1;
}
}
void getNewTargetY(){
tarY = int (random(width));
while (curY == tarY) {
tarY = int (random(width));
}
if (curY > tarY) {
signY = -1;
}
else
{
signY = 1;
}
}
// LR vertex ==============================
void getNewTargetX1() {
tarX1 = int (random(width));
while (curX1 == tarX1) {
tarX1 = int (random(width));
}
if (curX1 > tarX1) {
signX1 = -1;
}
else
{
signX1 = 1;
}
}
void getNewTargetY1(){
tarY1 = int (random(width));
while (curY1 == tarY1) {
tarY1 = int (random(width));
}
if (curY1 > tarY1) {
signY1 = -1;
}
else
{
signY1 = 1;
}
}
// LL vertex items ========================
void getNewTargetX2() {
tarX2 = int (random(width));
while (curX2 == tarX2) {
tarX2 = int (random(width));
}
if (curX2 > tarX2) {
signX2 = -1;
}
else
{
signX2 = 1;
}
}
void getNewTargetY2() {
tarY2 = int (random(width));
while (curY2 == tarY2) {
tarY2 = int (random(width));
}
if (curY2 > tarY2) {
signY2 = -1;
}
else
{
signY2 = 1;
}
}
// end of code
Any ideas??
Thanks,
George
15" MacBook Pro, 2.4GHz Intel Core i5, OS X v.10.8.3 Mountain Lion,
QuickTime Player v.10.2, Processing v. 1.5.1 and v.2.0b8, Arduino Uno, Nano v.3.
1