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 › which for loop for my visual
Page Index Toggle Pages: 1
which for loop for my visual ? (Read 425 times)
which for loop for my visual ?
Jun 3rd, 2008, 10:36pm
 
Hello!
and Thanks for your help.
I´ve wrote very simple codes
which I want to combine with eachother.

The first draws an ellipse that floats from the left end of the screen to the right one. Always on the height of the mouse cursor. The gradient tail is just a kind of cheating-visual. You can disable this if you run background within draw. ((Which is at the moment just a comment)
//--------------------------------------------------------

float myVariable = 25f; // 1.000045345
float myPosX = 0.6f;

void setup() {
size(400,200);
background(0);
frameRate(30);
}


void draw() {
//background(0);
myPosX = myPosX + 1f;

if(myPosX > 400) {
myPosX = 0;
}

noStroke();
fill(0,myVariable);
rect(0,0,400,200);

fill(255,255,255);
ellipse(myPosX,mouseY,10,10);
}

//-----------------------------------------------------


The second is like a tool for showing that 2 objects are in Proximiti. In this case
if the cursor is in the near of the point
a line gets drawn between them.
//----------------------------------------------------------

void setup (){
 size (800,800);
 noCursor();
}

void draw (){
 background(0);
 int pointX = 200;
 int pointY = 200;
 fill(255);
 ellipse (pointX,pointY,10,10); //point in the middle  

 if ((mouseX>pointX-200 & mouseX<pointX+200) &&
   (mouseY>pointY-200 & mouseY<pointY+200)) {  

   stroke(255);
   line(mouseX,mouseY,pointX,pointY);
 }

 noStroke();
 fill(255);
 ellipse(mouseX,mouseY,30,30);  
}

//----------------------------------------------------------

I want to combine theese two codes like that:
If my cursor is in proximity to the point (like in the second code) an ellipse floats from the point to the cursor,instead of linking them by a line. So what´s the line in the second code shall be a floating ellipse from object(point) to the cursor.

So i have no idea how to do this.
First I thought about trying it with an for loop that grows the X and Y coordinates of the floating ellipse untill they reach the coordinates of my cursor... But I´m not sure how to write this in a good way...

If anyone (especially PhiLo) has any suggestions I would be very thankfull.
Re: which for loop for my visual ?
Reply #1 - Jun 4th, 2008, 5:18am
 
Quote:


float pointX = 200;  
float pointY = 200;  
float distance = 100;  // distance from mouse pointer
float deltaX, deltaY;  

void setup (){
 size (800,800);
 noCursor();
}

void draw (){
 background(0);
 deltaX= mouseX - pointX;
 deltaY= mouseY - pointY;
 pointX+=1;
 if (pointX > width){
   pointX=0;
 }
 fill(255);  
 ellipse (pointX,pointY,10,10); //punkt in der mitte  
 if ((deltaX < distance) && (deltaX < distance)) {  
   pointX+= deltaX/20f;
   pointY+= deltaY/20f;
   stroke(255);
   line(mouseX,mouseY,pointX,pointY);
 }  

 noStroke();
 fill(255);
 ellipse(mouseX,mouseY,30,30);    
}


Re: which for loop for my visual ?
Reply #2 - Jun 4th, 2008, 11:22am
 
thanks for your code mate!
Really nice of you.

Unforunately I thought of the second code but just replacing the line with an floating ellipse (like if they get in proximity the point gives information visualized by floating ellipses to the other object,cursor or whatever).
And it seems that your code doesnt work if the cursor is approximating from the feft side.
Which is a huge problem for me. I really got no idea how to write this.

I´m really not lazy but  there are many things I yet don´t know and if I try to solve that I would need a week or so.
where experienced people in this forum have a look at this and say : aahh I know this guys problem! it´s very simple to solve...
So it´s just a better way of learning for me. And if I will be able to share my knowledge some time I surely will do that.


So thank you very much for your help.

But any further suggestions are welcome.

Re: which for loop for my visual ?
Reply #3 - Jun 4th, 2008, 3:37pm
 
there's a typo:

if ((deltaX < distance) && (deltaX < distance)) {  

should use deltaY in the second bit
Re: which for loop for my visual ?
Reply #4 - Jun 4th, 2008, 4:35pm
 
Thanks !
It works better now but its not the same distance everywhere thats needet to get linked with the point. For examole if you approximate from the the right under the point, the needet distance is very short. But if you approximate from the top left you can get linked with the point in every distance.
Even if the point is at the absolut bottom right and the cursor at the absolut top left, they get linked.

And once more ...
I thought of this code but replacing the line with an ellipse floating from the point to my cursor (NOT letting the point itself float towars the cursor !)

void setup (){
 size (800,800);
 noCursor();
}

void draw (){
 background(0);
 int pointX = 200;
 int pointY = 200;
 fill(255);  
 ellipse (pointX,pointY,10,10); //point in the middle  

 if ((mouseX>pointX-200 & mouseX<pointX+200) &&
   (mouseY>pointY-200 & mouseY<pointY+200)) {  

   stroke(255);
   line(mouseX,mouseY,pointX,pointY);
 }  

 noStroke();
 fill(255);
 ellipse(mouseX,mouseY,30,30);    
}
Re: which for loop for my visual ?
Reply #5 - Jun 4th, 2008, 6:47pm
 
yes, the distance check does look a bit wrong still.

usually distance is calculated using pythagorus.

xDiff = xCursor - xCircle
yDiff = yCursor - yCircle
distance = (xDiff * xDiff) + (yDiff * yDiff)

if (distance < threshold)
...

(where threshold is the minimum distance squared)

this is wrong:

if ((mouseX>pointX-200 & mouseX<pointX+200) &&
   (mouseY>pointY-200 & mouseY<pointY+200)) {

those single &s between the variables are bitwise operators, not booleans - http://processing.org/reference/bitwiseAND.html
Re: which for loop for my visual ?
Reply #6 - Jun 4th, 2008, 7:47pm
 
sorry for the typos, it was a quick copy paste job that i only tested once.  Well glad to see you sorted it out
Page Index Toggle Pages: 1