Matt2071 122 Report post Posted November 11, 2001 Hi, I''m trying to do a 2D rotation of a triangle in a Java applet, which uses same coordinate system as windows. --------------------x+ | | | | | | | y+ I have tried several different ways of handling this and finally opened up tricks of the windows game programming gurus and found lamothe''s 2d rotation function: public void rotate(int num, int degrees) { int cos = (int) Math.cos(degrees); int sin = (int) Math.sin(degrees); for (int current_vertices=0;current_vertices<3;current_vertices++) { xr =(xdata_triangle[num][current_vertices]*cos - ydata_triangle[num][current_vertices]*sin); yr =(xdata_triangle[num][current_vertices]*sin + ydata_triangle[num][current_vertices]*cos); xdata_triangle[num][current_vertices] = xr; ydata_triangle[num][current_vertices] = yr; } } num represents the id of the trianlge I am trying to rotate and current_vertices is each point on the triangle. degrees is of course the degrees of rotation. I don''t fully understand the part of this function on how he gets xr and yr but this hasnt been working for me. It simply sets all of the coordinates to 0. Sorry if this should go under Java development but it seemed it would be better here to me. Thanks 0 Share this post Link to post Share on other sites

olp-fan 122 Report post Posted November 11, 2001 Dude... you''re casting your sine and cos functions to an (int) and storing it in an int. Didn''t you know that sine and cos have a range of [-1,1]. Which means it will (almost) always evaluate to 0 when casted to an int. Instead make it a double!so...double cos = Math.cos(degrees);double sin = Math.sin(degrees);And.... Math.cos and Math.sin take the angle in radians, so multiply by PI/180 first. So try thisdouble cos = Math.cos(degrees*Math.PI/180);double sin = Math.sin(degrees*Math.PI/180);Dude, its obvious you haven''t taken beyond grade 11 math, and you don''t copy code from the book very well. Anyways, good luck.- Tom 0 Share this post Link to post Share on other sites

Matt2071 122 Report post Posted November 12, 2001 Sorry. Reason it''s cased to an int is because I can''t have double''s for my coords when I draw polygon.I know it''s in radians but I cut it down code some to make it simpler.The math wasn''t throwing me off...Thank you for your help, sir. 0 Share this post Link to post Share on other sites

Matt2071 122 Report post Posted November 12, 2001 I''m sorry that I''m so lame to you. I changed the code like you said to use double and then I cased to int as I was putting the coords back in. Only problem with this is that it moves the entire figure over (x decreases, y increases). Is this because of the casing again or something else? Not sure how else to go around it since it needs integer value.Here''s what I mean if I''m not clear:xdata_triangle[num][current_vertices] = (int)xr;ydata_triangle[num][current_vertices] = (int)yr; 0 Share this post Link to post Share on other sites

olp-fan 122 Report post Posted November 12, 2001 I''m not too sure what you mean, but that code should be right. The only thing that I would consider is making it go wrong is how you conceptualize it. Those rotation formula''s are rotating a point around the origin... which is the upper left corner of your window. To make it rotate around its centre, define the points of the polygon around a "logical" origin defined however you like, then use a translation formula (or matrix) and put the polygon wherever you like.Send me the code if you still have problems, but you need to cast it to an int before you draw, that much is true.And you don''t have to call me sir, I''m just in University =) I''m not THAT old.- Tom 0 Share this post Link to post Share on other sites

Matt2071 122 Report post Posted November 13, 2001 Thank you very much for your help. It only moves over a bit so I don't really think that if I added back the original coordinates that it would be in the correct place. Before I used this function, I was doing that but it was my understanding that in this function, it rotated around a point on z plane. (or at least that's the way it's thought of.) public void rotate(int num, int degrees){double cos = Math.cos(degrees*Math.PI/180);double sin = Math.sin(degrees*Math.PI/180);for (int current_vertices=0;current_vertices<3;current_vertices++){double xr =(xdata_triangle[num][current_vertices]*cos -ydata_triangle[num][current_vertices]*sin);double yr =(xdata_triangle[num][current_vertices]*sin + ydata_triangle[num][current_vertices]*cos);xdata_triangle[num][current_vertices] = (int) xr;ydata_triangle[num][current_vertices] = (int) yr;}} The triangle has a set_starting_points() function that does something like this: public void set_starting_points(){ xdata_triangle[0][0] = 20; ydata_triangle[0][0] = 20; xdata_triangle[0][1] = 20; ydata_triangle[0][1] = 40; xdata_triangle[0][2] = 40; ydata_triangle[0][2] = 40;} arrays are setup like this (i think): int xdata_triangle[][] = new int[5][3];int ydata_triangle[][] = new int[5][3]; and I use a mouse event to call rotate on the triangle if themouse is pressed inside that triangle. like this: public MousePressed(MouseEvent me){ int x = me.getX(); int y = me.getY(); int which_triangle = in_triangle(x,y);(returns the number inthe array that is the triangle mouse was pressed on or returnsa -1 if wasn't pressed inside any of the triangles)if(which_triangle != -1){ rotate(which_triangle,10);}} Sorry I wrote it all out like this but I don't have the code with me at the moment. I'll send it to you later if you'd like. Thank you very much for your help. It is much appreciated. This is a good learning experience for me no matter how easy it is Edited by - Matt2071 on November 13, 2001 11:36:53 AM 0 Share this post Link to post Share on other sites