Having problem with drawing the line by having two coordinates!!!
in
Programming Questions
•
2 years ago
Hi i am having problem with drawing the line from the previously mouse clicked and current mouse clicked location.
I got the distance but cant get the line. my code is as follwoing.
int x1 = 0,x2 = 0,y1 = 0,y2 = 0;
float TotalDistance;
boolean flag=false;
void setup()
{
size(600,600);
background(255);
}
void draw()
{
}
void mouseClicked()
{
if(flag==true)
{
x2 = mouseX;
y2 = mouseY;
TotalDistance=distance(x1,x2,y1,y2);
DrawLine(x1,x2,y1,y2);
//line(x1,y1,x2,y2);
background(255);
textSize(30);
fill(255,0,0);
text(TotalDistance, 50,20);
x1 = 0;
y1 = 0;
x2 = 0;
y2 = 0;
flag = false;
}
else
{
x1 = mouseX;
y1 = mouseY;
flag = true;
}
}
float distance(int x1,int x2,int y1,int y2)
{
float TotalDistance = sqrt(sq(x2-x1) + sq(y2-y1));
return TotalDistance;
}
void DrawLine(int x1,int x2,int y1,int y2)
{
stroke(125);
line(x1,y1,x2,y2);
}
I got the distance but cant get the line. my code is as follwoing.
int x1 = 0,x2 = 0,y1 = 0,y2 = 0;
float TotalDistance;
boolean flag=false;
void setup()
{
size(600,600);
background(255);
}
void draw()
{
}
void mouseClicked()
{
if(flag==true)
{
x2 = mouseX;
y2 = mouseY;
TotalDistance=distance(x1,x2,y1,y2);
DrawLine(x1,x2,y1,y2);
//line(x1,y1,x2,y2);
background(255);
textSize(30);
fill(255,0,0);
text(TotalDistance, 50,20);
x1 = 0;
y1 = 0;
x2 = 0;
y2 = 0;
flag = false;
}
else
{
x1 = mouseX;
y1 = mouseY;
flag = true;
}
}
float distance(int x1,int x2,int y1,int y2)
{
float TotalDistance = sqrt(sq(x2-x1) + sq(y2-y1));
return TotalDistance;
}
void DrawLine(int x1,int x2,int y1,int y2)
{
stroke(125);
line(x1,y1,x2,y2);
}
1
