Hello all!
I just wrote a class for curve.
Could somebody check / proofread my code?
I am surprised why the curve goes UP when my the control point goes down.
my understanding of https://www.processing.org/reference/curve_.html is that the 8
parameters are :
- controlpoint1 x,y
- anchor 1 x,y
- anchor 2 x,y
- controlpoint2 x,y
Thank you!
Warm regards,
Chrisir
// Test for a new Curve Class that can do curve and curvepoint (2D)
CurveClass myCurve;
// ---------------------------------------------------------------------------------------------
void setup() {
size(1500, 900);
final int STANDARD_DISTANCE=100;
myCurve = new CurveClass (
width/2-31, height/2+310, // beginning control point (cp1)
width/2-STANDARD_DISTANCE, height/2+STANDARD_DISTANCE, // coordinates for the first point
width/2+STANDARD_DISTANCE, height/2+STANDARD_DISTANCE, // coordinates for the second point
width/2+31, height/2+310 // ending control point
);
}
void draw() {
background(#24B41D);
fill(0);
text("You can drag the two red control points with the mouse. \nMove the mouse to move the white ball on the curve (mouseX->amt)",
14, 14);
testCurve();
}
//-------------------------------------------------------------------------------------------------------
void testCurve () {
myCurve.displayCurve();
myCurve.testCurveData();
myCurve.moveOnCurveWithMouseX(); // OR moveOnCurve()
myCurve.drag();
}
//-------------------------------------------------------------------------------------------------------
void mousePressed() {
myCurve.mousePressedClass();
}
void mouseReleased() {
myCurve.mouseReleasedClass();
}
// ===============================================================================================
class CurveClass {
PVector cp1, anchor1, anchor2, cp2;
float x, y;
float amt;
int drag = -1;
// constr - you call the constr as you would curve command (2D version)
CurveClass ( float f0, float f1,
float f2, float f3,
float f4, float f5,
float f6, float f7 ) {
// constr
cp1 = new PVector (f0, f1);
anchor1 = new PVector (f2, f3);
anchor2 = new PVector (f4, f5);
cp2 = new PVector (f6, f7);
} // constr
// ---
void displayCurve() {
stroke(255, 0, 0); //RED
strokeWeight(1);
noFill();
curve (
cp1.x, cp1.y,
anchor1.x, anchor1.y,
anchor2.x, anchor2.y,
cp2.x, cp2.y);
}
// ---
void moveOnCurve() {
// AUTO move
x = curvePoint(cp1.x, anchor1.x, anchor2.x, cp2.x, amt);
y = curvePoint(cp1.y, anchor1.y, anchor2.y, cp2.y, amt);
fill(255); // WHITE
noStroke();
ellipse(x, y, 5, 5);
amt+=0.01;
if (amt>=1)
amt=0.0;
}
void moveOnCurveWithMouseX() {
// mouseX move
amt=map(mouseX, 0, width,
0, 1);
x = curvePoint(cp1.x, anchor1.x, anchor2.x, cp2.x, amt);
y = curvePoint(cp1.y, anchor1.y, anchor2.y, cp2.y, amt);
fill(255); // WHITE
noStroke();
ellipse(x, y, 5, 5);
//amt+=0.01;
//if (amt>=1)
// amt=0.0;
}
// ---
void testCurveData() {
noStroke();
fill(255, 0, 0);
ellipse (cp1.x, cp1.y, 28, 18);
text("cp1",
cp1.x+15, cp1.y-8);
noStroke();
fill(255, 0, 0);
ellipse (cp2.x, cp2.y, 28, 18);
text("cp2",
cp2.x+15, cp2.y-8);
//-------
noStroke();
fill( 0, 0, 255);
ellipse (anchor1.x, anchor1.y, 28, 18);
noStroke();
fill( 0, 0, 255);
ellipse (anchor2.x, anchor2.y, 28, 18);
}//method
// ---
void mousePressedClass() {
if (dist(mouseX, mouseY, cp1.x, cp1.y) < 33)
drag=0;
else if (dist(mouseX, mouseY, cp2.x, cp2.y) < 33)
drag=1;
}//method
void mouseReleasedClass() {
drag=-1; // reset
}
// ---
void drag() {
switch (drag) {
case 0:
cp1.x=mouseX;
cp1.y=mouseY;
break;
case 1:
cp2.x=mouseX;
cp2.y=mouseY;
break;
case -1:
//ignore
break;
}//switch
}//method
//
}//class
//