FAQ
Cover
This is the archive Discourse for the Processing (ALPHA) software.
Please visit the new Processing forum for current information.

   Processing 1.0 _ALPHA_
   Programming Questions & Help
   Programs
(Moderators: fry, REAS)
   moveTo for shape
« Previous topic | Next topic »

Pages: 1 
   Author  Topic: moveTo for shape  (Read 340 times)
kevinP

Email
moveTo for shape
« on: Jun 7th, 2004, 2:21pm »

Hi all,
 
I am trying to draw some simple polygons using side lengths and angles (somewhat like turtle graphics do).
 
I thought that this is not much more than converting polar coords to Cartesian, but in my simplified example below I get a diamond (or rotated square) rather than a square parallel to my X/Y axes. The vertices are being calculated relative to the central starting point.
 
Code:

void setup()
{
 size(300,300);
}
 
// square
int[] side = { 100, 100, 100, 100 }; // length of sides
int[] dir = { 0, -90, -180, -270 }; // direction in deg.
 
void draw()
{
 display();
}
 
void display()
{
 translate(width/2, height/2);
 beginShape(POLYGON);
 {
  for(int i=0; i<side.length; i++) {
  vertex(side[i] * cos(radians(dir[i])),  
  side[i] * sin(radians(dir[i])));
  translate(side[i] * cos(radians(dir[i])),
  side[i] * sin(radians(dir[i])));
  }
 }
 endShape();
}

 
Follow-up - using this and no beginShape() works fine:
line(0,0, cos(angle), sin(angle));
translate(cos(angle), sin(angle));
 
But I want to be able to draw a filled polygon (using side length and angle).
« Last Edit: Jun 7th, 2004, 2:47pm by kevinP »  

Kevin Pfeiffer
TomC

WWW
Re: moveTo for shape
« Reply #1 on: Jun 7th, 2004, 2:49pm »

You're only drawing one vertex per line.
 
line(x1,y1,x2,y2) is the equivalent of vertex(x1,y1) and vertex(x2,y2).
 
« Last Edit: Jun 7th, 2004, 2:50pm by TomC »  
kevinP

Email
Re: moveTo for shape
« Reply #2 on: Jun 7th, 2004, 2:58pm »

I realized that I need to save my "current position" and then add that to each new X/Y calculation in order to get vertices relative to each other.
 
This works...
Code:

void display()
{
  int xPos = 0;
  int yPos = 0;
   
  translate(width/2, height/2);
  beginShape(LINE_LOOP);
  {
    vertex(xPos, yPos);
    for(int i=0; i<side.length; i++) {
 xPos += int( side[i] * cos(radians(dir[i])) );
 yPos += int( side[i] * sin(radians(dir[i])) );
 vertex(xPos, yPos);
 println("xPos = " + xPos + "  yPos = " + yPos);
    }
  }
  endShape();
}
« Last Edit: Jun 7th, 2004, 3:13pm by kevinP »  

Kevin Pfeiffer
TomC

WWW
Re: moveTo for shape
« Reply #3 on: Jun 7th, 2004, 3:01pm »

True.  On second thought I think that the problem is with calling translate inside beginShape/endShape.
 
Quote:

Transformations such as translate(), rotate(), and scale() do not work within beginShape().

 
-- http://processing.org/reference/beginShape_.html
 
kevinP

Email
Re: moveTo for shape
« Reply #4 on: Jun 7th, 2004, 3:14pm »

Yes, I see. Also sorry for stepping on your reply -- I updated my previous posting which threw your answer here out of context.
 
-K
 

Kevin Pfeiffer
Pages: 1 

« Previous topic | Next topic »