We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpPrograms › Help Manipulating array objects
Page Index Toggle Pages: 1
Help Manipulating array objects (Read 566 times)
Help Manipulating array objects
Jul 25th, 2008, 9:54pm
 
I just recently started using processing and it has been a while since I have programed anything at all but I'm having a lot of fun learning it.. keeping that in mind, my code may have some redundancies due to my lack of experience.

I have been writing practice programs to explore the functionality of the language and was wondering if it's possible to manipulate each individual object (with the mouse) in an array without defining the area for the mouse to be over. (i.e. make 1 line in an array have a strokeWeight of 3 while the rest have a strokeWeight of 1)

And if that is not possible could someone help me with the math to define the entire area of a diagonal line? I am a bit rusty. (though I will be working on this constantly until I figure it out).



//removed code as redundant.. please use the code in the following post.
Re: Help Manipulating array objects
Reply #1 - Jul 29th, 2008, 5:26pm
 
I've gotten a lot further and have been able to greatly reduce the amount of code necessary which should make it easier to read.

My only problem at the moment is that the program only functions correctly in the upper left quarter. I think I need to alter the 'if' statement inside boolean overLine to correct this but I'm not sure of the math. Could someone please help.

I am currently using 'y = mx +b' to try and plot the line.

Code:

int numLines;
float radius;
int stro;
Lines[] ln;

void setup(){
size(500, 500);
background(0);
stroke(255);
smooth();

//the equation below will make the lines extend to the edges
radius = width/2-10; //sqrt(pow(width/2, 2)+pow(height/2,2));
numLines = round(degrees(TWO_PI)/3);
ln = new Lines[numLines];

for (int i = 0; i<ln.length; i++){
float angle = radians(i*3);
float x = width/2 +(cos(angle) * radius); //equation for the circle
float y = width/2 +(sin(angle) * radius); //that defines the lines.
ln[i] = new Lines(x, y, ln);
}

}

void draw(){
fill(0, 12);
rect(-1, -1, width+1, height +1);
fill(255);
stroke(255);
for (int i = 0; i <ln.length; i++){
ln[i].update();
ln[i].display();

}
}

class Lines{
float x, y, m;
Lines[] others;
boolean over;
boolean locked = false;
boolean otherslocked = false;

Lines(float xpos, float ypos, Lines[] o){
x = xpos;
y = ypos;
m = y/x;
others = o;

}

void update(){
for (int i = 0; i<others.length; i++){
if (others[i].locked == true){
otherslocked = true;
break;
}
else {
otherslocked = false;
}
}

if (otherslocked == false){
over();
}
}

void over(){
if (overLine(x, y, m)){
over = true;
stro = 3;
}
else{
over = false;
stro = 1;
}
}

void display(){

if (over) {
strokeWeight(stro);
line(x, y, width/2, height/2);
}

strokeWeight(1);
line(x, y, width/2, height/2);
}
}

boolean overLine(float x, float y, float m){
if (mouseX >= y/m && mouseY >= m*x){ //if I change the '>' for '<' I can change
return true; //which quadrant it will respond to the
} //mouse properly in
else {
return false;
}
}


Re: Help Manipulating array objects
Reply #2 - Jul 31st, 2008, 9:55am
 
I think java.awt.geom package and more precisely java.awt.geom.Line2D class has built-in methods to get the distance from a point (mouse cursor in your case) to a line.

See this link :

http://java.sun.com/j2se/1.4.2/docs/api/java/awt/geom/Line2D.html#ptLineDist(dou...

Assuming you're using this Line2D class to store your segments coordinates, your overLine() method could look like :

Quote:
boolean overLine(java.awt.geom.Line2D line2d) {
 return (line2d.ptLineDist((double)mouseX, (double)mouseY) < 5);
}
Page Index Toggle Pages: 1