Java Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesJava Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old November 1st, 2012, 07:49 PM
Shaun_B Shaun_B is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2012
Posts: 62 Shaun_B User rank is Corporal (100 - 500 Reputation Level)Shaun_B User rank is Corporal (100 - 500 Reputation Level)Shaun_B User rank is Corporal (100 - 500 Reputation Level)Shaun_B User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 1 Day 52 m 17 sec
Reputation Power: 2
Swing - Rotating a 2D shape from an array of points.

Hi folks,

I'm looking into shape rotations around the centre point of a wire-frame shape to try and improve my understanding of mathematics. Unfortunately, I'm not getting very far...!

Here's the shape that I want to rotate:
Java Code:
Original - Java Code
  1.     private static int star[] =
  2.     {
  3.         /** co-ordinates in array read as
  4.          * x0, y0 to x1, y1. -1 terminates */
  5.          0, 28, 30, 28,
  6.         30, 28, 390,
  7.         390, 50, 28,
  8.         50, 28, 79, 28,
  9.         79, 28, 55, 46,
  10.         55, 46, 64, 73,
  11.         64, 73, 40, 57,
  12.         39, 57, 15, 73,
  13.         15, 73, 23, 45,
  14.         23, 450, 28,
  15.         -1, -1
  16.     };

and I'm drawing it like this:
Java Code:
Original - Java Code
  1.     public void drawImage(int x, int y)
  2.     {
  3.         backg.setColor(Color.black);
  4.         // This will step through the array points to draw
  5.         // the star object:
  6.         while(star[index]>=0)
  7.         {
  8.             int x0 = x+(star[index+0]);
  9.             int y0 = y+(star[index+1]);
  10.             int x1 = x+(star[index+2]);
  11.             int y1 = y+(star[index+3]);
  12.             backg.drawLine( x0, y0, x1, y1 );
  13.             index += 4;
  14.         }
  15.         // Resets index to zero:
  16.         index = 0;
  17.     }

It draws fine until I try to rotate the points; x and y are used to offset the position accordingly.

Anyway, my rotate method is:
Java Code:
Original - Java Code
  1.     public void rotate()
  2.     {
  3.         // Divides width and height in half
  4.         double px=widthOfStar>>>1;
  5.         double py=heightOfStar>>>1;
  6.         // Sets up variables for the maths:
  7.         double x2=0.00;
  8.         double y2=0.00;
  9.         double x1=0.00;
  10.         double y1=0.00;
  11.         // Degrees to turn object in radians:
  12.         double degrees = Math.toRadians(6);
  13.         while(star[index]>=0)
  14.         {
  15.             x1 = star[index+0];
  16.             y1 = star[index+1];
  17.             x2 = px+(px-x1)*Math.cos(degrees)-(py-y1)*Math.sin(degrees);
  18.             y2 = py+(px-x1)*Math.sin(degrees)+(py-y1)*Math.cos(degrees);
  19.             star[index+0]=(int)x2;
  20.             star[index+1]=(int)y2;
  21.             index += 2;
  22.         }
  23.         index = 0;
  24.     }

It definitely rotates something, and it is rotating around the centre of the object, but the shape is not keeping its' err... shape, or at least not for me :-\

There's either something that I've misunderstood, or my code is rubbish. Can anyone help please?

Many thanks,

Shaun.

Reply With Quote
  #2  
Old November 1st, 2012, 09:39 PM
Corpsecreate Corpsecreate is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2010
Posts: 38 Corpsecreate User rank is Private First Class (20 - 50 Reputation Level)Corpsecreate User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 1 Day 2 h 7 m 3 sec
Reputation Power: 3
OH MY GOD are you seriously trying to draw stars and rotate them? I tried to do this for my assignment in first year. I got it working and I'll post the code below but please for the love of god dont ask me to explain it. Somehow, back when I was smart, I managed to calculate all the required maths to rotate it. It was HELL.

java Code:
Original - java Code
  1. import java.awt.*;
  2. import java.applet.Applet;
  3. public class StarsRotation extends Applet
  4. {
  5.     public void paint (Graphics page)
  6.     {
  7.         setBackground(Color.black);
  8.         double angle = 180/Math.PI;
  9.         int stars = 100;
  10.         for (int i = 0; i < stars ; i++)
  11.         {
  12.             int theta = (int) (73 * Math.random());
  13.             int randomX = (int) (1463 * Math.random()) + 72;
  14.             int randomY = (int) (940 * Math.random()) + 100;
  15.             int [] xCoords, yCoords;
  16.             int radius = (int) ( 50 * Math.random()+5);
  17.             int X1 = (int) (Math.round(randomX + radius*Math.sin(theta/angle)));
  18.             int X2 = (int) (Math.round(randomX + (radius*Math.sin(18/angle)*Math.sin((36+theta)/angle))/Math.sin(126/angle)));
  19.             int X3 = (int) (Math.round(randomX + radius*Math.sin((108-theta)/angle)));
  20.             int X4 = (int) (Math.round(randomX + (radius*Math.sin(18/angle)*Math.sin((72-theta)/angle))/Math.sin(126/angle)));
  21.             int X5 = (int) (Math.round(randomX - radius*Math.sin((theta-36)/angle)));
  22.             int X6 = (int) (Math.round(randomX - (radius*Math.sin(theta/angle)*Math.sin(18/angle))/Math.sin(126/angle)));
  23.             int X7 = (int) (Math.round(randomX - radius*Math.sin((36+theta)/angle)));
  24.             int X8 = (int) (Math.round(randomX - (radius*Math.sin(18/angle)*Math.sin((108-theta)/angle)/Math.sin(126/angle))));
  25.             int X9 = (int) (Math.round(randomX - radius*Math.sin((72-theta)/angle)));
  26.             int X10 = (int) (Math.round(randomX + (radius*Math.sin(18/angle)*Math.sin((theta-36)/angle))/Math.sin(126/angle)));
  27.             int Y1 = (int) (Math.round(randomY-radius*Math.cos(theta/angle)));
  28.             int Y2 = (int) (Math.round(randomY - (radius*Math.sin(18/angle)*Math.cos((36+theta)/angle))/Math.sin(126/angle)));
  29.             int Y3 = (int) (Math.round(randomY + radius*Math.cos((108-theta)/angle)));
  30.             int Y4 = (int) (Math.round(randomY + (radius*Math.sin(18/angle)*Math.cos((72-theta)/angle))/Math.sin(126/angle)));
  31.             int Y5 = (int) (Math.round(randomY + radius*Math.cos((theta-36)/angle)));
  32.             int Y6 = (int) (Math.round(randomY + (radius*Math.cos(theta/angle)*Math.sin(18/angle))/Math.sin(126/angle)));
  33.             int Y7 = (int) (Math.round(randomY + radius*Math.cos((36+theta)/angle)));
  34.             int Y8 = (int) (Math.round(randomY - (radius*Math.sin(18/angle)*Math.cos((108-theta)/angle)/Math.sin(126/angle))));
  35.             int Y9 = (int) (Math.round(randomY - radius*Math.cos((72-theta)/angle)));
  36.             int Y10 = (int) (Math.round(randomY - (radius*Math.sin(18/angle)*Math.cos((theta-36)/angle))/Math.sin(126/angle)));
  37.             xCoords = new int[] {X1, X2 , X3, X4 , X5 , X6 , X7 , X8 , X9 , X10};
  38.             yCoords = new int[] {Y1, Y2 , Y3, Y4 , Y5 , Y6 , Y7 , Y8 , Y9 , Y10};
  39.             int randomRed = (int) (225 * Math.random()+30);
  40.             int randomGreen = (int) (225 * Math.random()+30);
  41.             int randomBlue = (int) (225 * Math.random()+30);
  42.             int randomTrans = (int) (225 * Math.random()+30);
  43.             page.setColor(new Color(randomRed, randomGreen, randomBlue, randomTrans));
  44.             page.drawPolygon (xCoords, yCoords, xCoords.length);
  45.             page.fillPolygon (xCoords, yCoords, 10);
  46.             page.drawPolygon (xCoords, yCoords, xCoords.length);
  47.         }
  48.     }
  49. }

Last edited by Corpsecreate : November 1st, 2012 at 09:43 PM.

Reply With Quote
  #3  
Old November 1st, 2012, 09:57 PM
slink's Avatar
slink slink is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2010
Posts: 73 slink User rank is Sergeant (500 - 2000 Reputation Level)slink User rank is Sergeant (500 - 2000 Reputation Level)slink User rank is Sergeant (500 - 2000 Reputation Level)slink User rank is Sergeant (500 - 2000 Reputation Level)slink User rank is Sergeant (500 - 2000 Reputation Level) 
Time spent in forums: 1 Day 17 h 10 m 20 sec
Reputation Power: 14
Hi Shaun_B

This is a bit of a guess but should it be:

Code:
x2 = px + (x1 - px) * Math.cos(degrees) + (y1 - py) * Math.sin(degrees);
y2 = py + (y1 - py) * Math.sin(degrees) - (x1 - px) * Math.cos(degrees); 


A good approach to fixing sticky bits of code is to break them out into a function

Code:
private double[] rotate(x1, y1, px, py, degrees) {...}


That way you can test it to death in isolation, then use it in your code once you are confident it is working.

Hope this helps,
slink

Reply With Quote
  #4  
Old November 2nd, 2012, 02:08 AM
Shaun_B Shaun_B is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2012
Posts: 62 Shaun_B User rank is Corporal (100 - 500 Reputation Level)Shaun_B User rank is Corporal (100 - 500 Reputation Level)Shaun_B User rank is Corporal (100 - 500 Reputation Level)Shaun_B User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 1 Day 52 m 17 sec
Reputation Power: 2
It's okay, I think that I've worked out the solution last night, or at least I think I did when I was trying to sleep after being in some discomfort. Basically, it's the way I've got my points in my array: there's repeating points which isn't a very good idea.

I'll have a solution shortly,

Shaun.

PS, You should always try to understand your homework, however, I'm doing this for a laugh.

Reply With Quote
  #5  
Old November 2nd, 2012, 07:15 AM
Shaun_B Shaun_B is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2012
Posts: 62 Shaun_B User rank is Corporal (100 - 500 Reputation Level)Shaun_B User rank is Corporal (100 - 500 Reputation Level)Shaun_B User rank is Corporal (100 - 500 Reputation Level)Shaun_B User rank is Corporal (100 - 500 Reputation Level) 
Time spent in forums: 1 Day 52 m 17 sec
Reputation Power: 2
The solution that I thought up is better, but it's not as perfect as I'd hoped. Although I got a simple rhombus to rotate around its' centre point, but not the star - okay, it is rotating, but it's not keeping it's shape.

Perhaps I need an identity matrix or something, or I'll probably have to speak to some games programmers as they really do understand this stuff.

Regards,

Shaun.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesJava Help > Swing - Rotating a 2D shape from an array of points.

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap