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 & HelpSyntax Questions › Object newbie - how to get x, y values
Page Index Toggle Pages: 1
Object newbie - how to get x, y values? (Read 551 times)
Object newbie - how to get x, y values?
Dec 2nd, 2008, 11:26am
 
Hi forum,
How do I get the x, y valus in this example? I think I have to handle the line as an object to ask for the values - but I am lost...

translate(width/2, height/2);
rotate((PI/180)*(random(360)));
line(0, 0, 50,50 );
//How to get the x and y values of the end point of the line, relating to the sketch?
//println(What on earth?!);
Re: Object newbie - how to get x, y values?
Reply #1 - Dec 2nd, 2008, 12:02pm
 
If I understand you right you want to have the screen coordinates of the lines end point after the transformation?

have you seen the screenX()/screenY() functions?

Re: Object newbie - how to get x, y values?
Reply #2 - Dec 2nd, 2008, 3:07pm
 
Thanx Ilu,
I tried out screenX()/screenY(). Here is my result - I still got problems...

void setup() {
 size(200, 200);
}

void draw() {
 background(255);
 
 float x1 = 0;
 float y1=  0;

 float x2 = 0;
 float y2 = 0;

 float x3 = 0;
 float y3 = 0;

 float x4 = 100;
 float y4 = 100;

 //Rotate the line -45 degrees
 rotate((PI/360)*-45);

 float theX1 = screenX(x1,y1);
 float theY1 = screenY(x2, y2);
 float theX2 = screenX(x3,y3);
 float theY2 = screenY(x4, y4);


 println("coordinates   x1:"+theX1+"  y1:"+theY1+"  x2:"+theX2+"  y2:"+theY2);
 
 strokeWeight(10);
 
 //but the line is drawn not in 45 degrees and photoshop says, the coordinates of the second x/y are also wrong...
 line(theX1, theY1, theX2,theY2);
}

Re: Object newbie - how to get x, y values?
Reply #3 - Dec 2nd, 2008, 4:55pm
 
You asked for the 'real' coordinates of the rotated lines end point. and that you would get with the screen() functions.

What are you doing now is you
1) rotate the 'world' you are drawing in
2) get the 'real' coordinates how they would be mapped on the screen and then
3) draw in the rotated 'world' using these coordinates...

maybe you could simply tell us what you really want to do Smiley
Re: Object newbie - how to get x, y values?
Reply #4 - Dec 2nd, 2008, 6:00pm
 
Sorry for confusing you folks...
Rotating the world first is no good idea, I guess Wink

What I like to do is this:
1)Drawing a line with the lenght 50px
2)Rotate this line with a random degree
3)Get the new start and end coordinates of the line

Re: Object newbie - how to get x, y values?
Reply #5 - Dec 3rd, 2008, 9:51am
 
if you use rotate() and the line() you don't simply rotate the line, you rotate the whole coordinate system.

so if you draw line(0,0,50,50) the 'virtual' start/end points are exactly 0x0 and 50x50 which map to a different screen position.

to get this screen position you can use screenX() and screenY().

I still don't get which of these two coordiates you need and for what?

if you simply want to add another line which starts were the first ends you simply use 50x50 as first point of the line...

Re: Object newbie - how to get x, y values?
Reply #6 - Dec 3rd, 2008, 7:44pm
 
This is the code where I need the coordinates of the end x/y of the lines.

What I like to have here, is to draw a line through all end x/y points of the lines and fill the shape whith a color.

int zoom=20;

// Array with the occurence of lottery numbers from 1 to 49 in 2002
int occurrence[]={
 8,3,7,4,8,9,14,9,6,8,10,6,6,3,3,9,7,7,5,4,5,4,4,7,4,5,10,4,8,8,5,3,3,11,3,7,5,8,
9,6,5,6,12,10,2,4,7,5,6
};

void setup()
{
 size(600, 600);
 noLoop();
 smooth();
 background(255);
}

void draw()
{
 translate(width/2, height/2);
 for (int i = 1; i < 50; i = i+1) {
   rotate((PI/180)*(360/49.0));
   strokeWeight((occurrence[i-1]/2));
   stroke(0);
   // draw 49 lines for the lottery numbers between 1 and 49
   // the lenght of the line stands for the occurence of the number
   line (0,0,(occurrence[i-1])*zoom,0);
 }
}

Re: Object newbie - how to get x, y values?
Reply #7 - Dec 4th, 2008, 10:33am
 
int zoom=20;

// Array with the occurence of lottery numbers from 1 to 49 in 2002
int occurrence[]={
 8,3,7,4,8,9,14,9,6,8,10,6,6,3,3,9,7,7,5,4,5,4,4,7,4,5,10,4,8,8,5,3,3,11, 3,7,5,8,9,6,5,6,12,10,2,4,7,5,6
};

int[] xes, ylons;

void setup()
{
 size(600, 600);
 noLoop();
 smooth();
 background(255);

 xes = new int[occurrence.length];
 ylons = new int[occurrence.length];
}

void draw()
{
 pushMatrix();
 translate(width/2, height/2);
 for (int i = 0; i < occurrence.length; i++) {
   rotate((PI/180)*(360/49.0));
   strokeWeight((occurrence[i]/2));
   stroke(0);
   // draw 49 lines for the lottery numbers between 1 and 49
   // the lenght of the line stands for the occurence of the number
   line (0,0,(occurrence[i])*zoom,0);
   xes[i] = (int) screenX(occurrence[i]*zoom, 0);
   ylons[i] = (int) screenY(occurrence[i]*zoom, 0);
 }
 popMatrix();

 stroke(255,0,0);
 strokeWeight(1);
 for(int i = 1; i < xes.length; i++) {
   line(xes[i-1], ylons[i-1], xes[i], ylons[i]);
 }
}


Re: Object newbie - how to get x, y values?
Reply #8 - Dec 4th, 2008, 9:11pm
 
Thank you so much Ilu (<-hero of the day)!
Works perfectly!
Smiley
Page Index Toggle Pages: 1