This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Polygon Rotation Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Polygon Rotation" Watch "Polygon Rotation" New topic
Author

Polygon Rotation

Ben Duffield
Greenhorn

Joined: Apr 15, 2004
Posts: 4
Hi there!
This class opens a DialogBox, which determines the Polygon's vertices. Now even-numbered vertices need to be rotated clockwise and odd-numbered vertices need to be rotated anti-clockwise!
For that I have created a second for-loop to take care of the odd-numbered vertices. I am however not sure what code exactly to write to make the odd-numbered vertices move anti-clockwise! The code for the for-loop that is supposed to make odd-numbered vertices move anti-clockwise is missing! I appreciate any help with this!
Below is the whole code.
Julie de Wet
Greenhorn

Joined: Apr 04, 2004
Posts: 17
Have you tried using -1 * angle ? i.e. negative angles.
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
I think
is being interpreted aswhen what you want is
It's a good idea to always use braces for if/for/while statements. At least until you're sure you understand what happens if you omit them. Note also that most IDEs (Eclipse, IntelliJ, NetBeans, JBuilder, etc) can format code for you - the indentation they use will make it obvious how the code is really being interpreted. For a beginner it's probably best to avoid IDEs initially to make sure you know how to write code yourself. But once you've mastered the essentials, and IDE can certainly help avoid these mistakes in the future. It's kind of like learning long division - you need to know how it works to really understand math, but after you've learned it you'll probably just use a calculator for anything complicated.


"I'm not back." - Bill Harding, Twister
Ben Duffield
Greenhorn

Joined: Apr 15, 2004
Posts: 4
Thanks for your posting!
The brackets don't make any difference here, but I know it's best to always include them. -1 * angle does not produce the desired effect. The Polygon just moves clockwise.
I have changed the algorithm of that section to the code below so as to only use one for-loop and keep it simple. But even the code below doesn't make the odd-vertices move anti-clockwise!
Julie de Wet
Greenhorn

Joined: Apr 04, 2004
Posts: 17
try and replace

with

[ April 18, 2004: Message edited by: Julie de Wet ]
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
The brackets don't make any difference here
Ummm, there's a huge difference between
and
Note that in the former, the indentation is wrong, wrong, wrong. I haven't tried to analyze what your code really does, or what you're trying to do, but I guarantee that if you thought the indentation was correct in your original code, then you need to use braces.
Ben Duffield
Greenhorn

Joined: Apr 15, 2004
Posts: 4
Thanks for all your help!
[code]
public static PlanarPoint negVertex (PlanarPoint c, double alpha, double r) {
double a = c.getX()-1*(r*Math.cos(alpha));
double b = c.getY()-1*(r*Math.sin(alpha));
PlanarPoint w = new PlanarPoint(a, b);
return w;
}
[code]
The code above solved the error! This is the code that was originally posted by me, but for some reason it threw an exception error. Now after creating the file again, it works.
I made an error saying that the brackets don't make any difference, but what I wanted to say was that I didn't notice any difference then after compiling, as the polygon was only still moving clockwise!
Anyway thanks for all the help!
 
jQuery in Action, 2nd edition
 
subject: Polygon Rotation
 
Similar Threads
Creating your own class
Non-static variable cannot be referenced from static context
Stopping Animation
Drawing a simple clock in java
Help in Algorithm Construction