Coordinate values arent stored in processing


Coordinate values arent stored in processing



I really have no idea why the x and y values wont go to the drawLines function


float x, x1, x2;
float y, y1, y2;
float rad; //radius
int lines = 30; //number of lines
int colorNumber = 1;

void setup() {
background(#FFFFFF);
size (800, 600);
rad = 8;
}

void draw() {
}



This creates the three dots or vertices of the mathematical envelope


void mouseClicked() {
float x = mouseX;
float x1 = mouseX;
float x2 = mouseX;
float y = mouseY;
float y1 = mouseY;
float y2 = mouseY;
if (colorNumber == 1) {
fill(#9393ff);
ellipse(x, y, rad, rad);
} else if (colorNumber == 2) {
fill(#FF9393);
ellipse(x1, y1, rad, rad);
} else if (colorNumber == 3) {
fill(#93ff93);
ellipse(x2, y2, rad, rad);
}
}



This is supposed to draw the envelope using the coordinates of the vertices


void drawLines(int numLines) {
for (int i = 0; i < numLines; i = i + 1) {
float x = mouseX;
float x1 = mouseX;
float x2 = mouseX;
float y = mouseY;
float y1 = mouseY;
float y2 = mouseY;
float t = (float) i/(numLines-1);
float startX = x + t * (x1 - x);
float startY = y + t * (y1 - y);
float endX = x1 + t * (x2 - x1);
float endY = y1 + t * (y2 - y1);
line (startX, startY, endX, endY);
}
}

void mouseReleased() {
colorNumber++;
if (colorNumber == 4) {
colorNumber = 1;
}
println(colorNumber);
}

void keyPressed() {
if (keyPressed == true) {
background(#FFFFFF);
}
}



this last stuff just tells the code if you press a key, it will reset the backround





It looks like you are shadowing the variables x and y - which ones do you want to to use - only declare them once? Although I have no idea from your code where the values mouseX etc are being set/declared
– Scary Wombat
Jun 28 at 0:25



x


y


mouseX





Well I have three coordinate pairs therefore 6 coordinate points (x,x1,x2,y,y1,y2) which are equal to the mouseX and mouseY values of each on of the three clicks respectively. you click a spot in the window and it creates a dot which is where mouseX and mouseY values come into play.
– Dylan
Jun 28 at 0:30





where is drawLines called from? Please add a minimal, complete, and verifiable example
– Scary Wombat
Jun 28 at 0:34



drawLines





I have called it from everywhere and it hasn't worked. As sad at it is I have been working on this for hours and can't figure out what is wrong
– Dylan
Jun 28 at 0:36





I have called it from everywhere and it hasn't worked so basically you have no idea? Please share the real code that you are using - this code does not called drawLines - Just noticed that you are using processing - correct?
– Scary Wombat
Jun 28 at 0:39


drawLines


processing




1 Answer
1



I understand your intention with using mouseX and mouseY to specify the coordinates of one of the 3 points of the envelope on click. The current issue is that all 3 points are being set to the same coordinate with each click. You need to introduce a variable to keep track of which coordinate to set on-click, such that only one pair is set. Then, only once all 3 coordinates are set, drawLines() can be called.



I propose the following:



Introduce 2 variables, one to keep track of which point is being modified; the other an array of PVectors (just to make it cleaner).


int index = 0;
PVector coords = new PVector[3];



Modify mouseClicked() to include the following:


void mouseClicked() {

ellipse(mouseX, mouseY, 8, 8);
coords[index] = new PVector(mouseX, mouseY);
index += 1;
if (index == 3) {
drawLines(lines);
}
index %= 3;
}



drawLines() becomes:


void drawLines(int numLines) {

for (int i = 0; i < numLines; i = i + 1) {
x = coords[0].x;
x1 = coords[1].x;
x2 = coords[2].x;
y = coords[0].y;
y1 = coords[1].y;
y2 = coords[2].y;
float t = (float) i / (numLines - 1);
float startX = x + t * (x1 - x);
float startY = y + t * (y1 - y);
float endX = x1 + t * (x2 - x1);
float endY = y1 + t * (y2 - y1);
line(startX, startY, endX, endY);
}
}



Finally, since your drawing on a black background, and the default stroke colour is black, use strokeColour() to change the colour of the lines so that you can see the envelope once its drawn.






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Moria Casán

How to make file upload 'Required' in Contact Form 7?

Quinn's Post Commonwealth War Graves Commission Cemetery