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 › rotating a shape made of points in its own center
Page Index Toggle Pages: 1
rotating a shape made of points in its own center (Read 571 times)
rotating a shape made of points in its own center
Jul 9th, 2009, 1:54am
 
Hi , i need to rotate a shape that is made of a bunch of point in its own center without using rotate.

The shape is made of hundreds of points so stored in an array , i need to access to the shape and rotate the points individually using a for structure in this way:

 for (int i = 0; i < numPoints; i++){
x[i] =
y[i] =
}

how can a make this?

many thanks




thanks




Re: rotating a shape made of points in its own center
Reply #1 - Jul 9th, 2009, 2:06am
 
> without using rotate.

why?

standard 2d rotation matrix is

Code:

Xrotated = X * COS(angle) - Y * SIN(angle)
Yrotated = X * SIN(angle) + Y * COS(angle)


but that's around the origin. to get it around the centre of the object you first need to work out the centre of the object (call this XC and YC) and then you need to translate by (-XC, -YC) before the rotate and by (XC, YC) after the rotate.

none of this is processing specific and there are plenty of references out there.
Re: rotating a shape made of points in its own center
Reply #2 - Jul 9th, 2009, 3:32am
 
koogy wrote on Jul 9th, 2009, 2:06am:
> without using rotate.

why

When you have such restriction, it is usually for homework: re-creating algorithms for the sake of understanding them. A good exercise...
Re: rotating a shape made of points in its own center
Reply #3 - Jul 9th, 2009, 5:36am
 
hi, its not homework its because im using the class glmodel from glgraphics library and i cannot use stuff like rotate in that class. Im still having some problems, when i apply too much rotation  it get distortion.
Any idea of why is this?

here is the code

Code:


import geomerative.*;
import javax.media.opengl.GL;
import processing.opengl.*;
import codeanticode.glgraphics.*;


GLCamera cam;
GLModel model;
float[] coords;
float[] colors;

float[] x;
float[] y;

float[] dx;
float[] dy;

float[] direccion;

float[] delta_x;
float[] delta_y;

float velocidad = 3;
//float direccion = random(TWO_PI);

int numPoints = 1154;

float distance = 4200;

//***********<geomerative>****************
RFont f;
RShape grp;
RPoint[] points;

RCommand curva = new RCommand(20 , 0, -400, -200, 200, -200, 0, -100);
RCommand curva_two = new RCommand(0 , -100, -400, -200, 200, -200, 0, -300);
RCommand[] piezas = new RCommand[5];
RCommand[] piezas_two = new RCommand[5];


int augment = 0;
float move_x ;
float move_y ;

int switcher = 0;



float mueve_letra_x = 48;
float mueve_letra_y = 38;



// mueve_letra_x es 48
// mueve_letra_y es 38;


//**********</geomerative>****************


void setup()
{
size(640, 480, GLConstants.GLGRAPHICS);
RG.init(this);

grp = RG.getText("a abc d a a a a a a a a ", "matteo.ttf", 12, CENTER);

points = grp.getPoints();
print(points);

cam = new GLCamera(this, 0, 0, distance, 0, 0, 0);

model = new GLModel(this, numPoints, POINTS, GLModel.DYNAMIC);
model.initColors();
x = new float[numPoints];
y = new float[numPoints];

dx = new float[numPoints];
dy = new float[numPoints];

delta_x = new float[numPoints];
delta_y = new float[numPoints];

direccion = new float[numPoints];

coords = new float[4 * numPoints];
colors = new float[4 * numPoints];

for (int i = 0; i < numPoints; i++)
{
x[i] = random(642);
y[i] = random(482);

dx[i] = random(642);
dy[i] = random(482);

direccion[i] = int(random(TWO_PI));

for (int j = 0; j < 3; j++) coords[4 * i + j] = 11 * random(-1, 1);
for (int j = 0; j < 3; j++) colors[4 * i + j ]= 0;
colors[4 * i + 3] = 0.5;
}
model.updateVertices(coords);
model.updateColors(colors);
}


void draw()
{
GLGraphics renderer = (GLGraphics)g;
GL gl = renderer.beginGL();
RG.setPolygonizer(RG.UNIFORMSTEP);
RG.setPolygonizerStep(4);

points = grp.getPoints();
augment = augment + 1;
piezas = curva.split(map(augment, 0, width, 0, 1));
piezas_two = curva_two.split(map(augment, 0, width, 0, 1));


if (switcher == 0 ) {
mueve_letra_x = piezas[0].endPoint.x;
mueve_letra_y = piezas[0].endPoint.y;


}
else if (switcher ==1) {
mueve_letra_x = piezas_two[0].endPoint.x;
mueve_letra_y = piezas_two[0].endPoint.y;
}


if (piezas[0].endPoint.x == 0 ) {
switcher = 1;
augment = 0;

}
noFill();
stroke(0,50);
curva.draw();
curva_two.draw();

stroke(255, 0, 0, 255);
ellipse(move_x , move_y, 10, 10);

stroke(0, 255, 0, 255);
piezas[1].draw();

piezas_two[1].draw();


//*******************</geomerative>*************


for (int i = 0; i < numPoints; i++){

float d = dist(x[i] , y[i] , points[i].x , points[i].y );
// float d = dist(x[i] , y[i] , points[i/52].x * -1 , points[i/52].y * -1);
if (mousePressed == true) {
delta_x[i] = 253 * (points[i].x + 239 - x[i] ) / d ;
delta_y[i] = 253 * (points[i].y + 288 - y[i] ) / d ;
}
else {
delta_x[i] = 0;
delta_y[i] = 0;
}

float radianes = radians(40);
direccion[i] = direccion[i] + random ( - radianes , radianes);
////////////////////////////////////////////////////////

dx[i] = velocidad * cos(direccion[i]);
dy[i] = velocidad * sin(direccion[i]);

x[i] = x[i] + dx[i]/6 + delta_x[i]/1 /1 ;
y[i] = y[i] + dy[i]/6 + delta_y[i]/1 / 1 ;

coords[4 * i ] = x[i]/6.5 - mueve_letra_x/5 - 64;

coords[4 * i + 1 ] = y[i]/6.5 * -1 + mueve_letra_y/5 + 64;

// Xrotated = X * COS(angle) - Y * SIN(angle)
//Yrotated = X * SIN(angle) + Y * COS(angle)
coords[4 * i ] = coords[4 * i ] * cos(mouseX * 0.003) - coords[4 * i + 1 ] * sin(mouseX * 0.003);
coords[4 * i + 1 ] = coords[4 * i] * sin(mouseX * 0.003) + coords[4 * i + 1 ] * cos(mouseX * 0.003);
}
model.updateVertices(coords);
cam.feed();
cam.clear(255, 255 , 255);
gl.glPointSize(1);
//gl.glLineWidth(8);
model.render();
cam.done();
renderer.endGL();
// println(frameRate);
}





Re: rotating a shape made of points in its own center
Reply #4 - Jul 9th, 2009, 5:40am
 
Can you just translate to your shape's origin, and then use glRotatef()?
Re: rotating a shape made of points in its own center
Reply #5 - Jul 9th, 2009, 7:38am
 
ohhhh , i added gl.glRotatef(augment * - 0.1, 0, 0, 0.5); before gl.glPointSize(1); and worked perfect. THANKS!!
Page Index Toggle Pages: 1