29 Replies  878 Views  Last Post: 30 August 2013  11:21 PM
#1
Question about math: keep track of a location in the Cartesian plane
Posted 28 August 2013  08:32 PM
Set the location to specified point?
Shift a location in a given amount along one of the axes, distance to another point
Rotate the location by a specified angle around the origin? (should I put the entire formula into code, so that whenever someone need to know the rotation, they only have to input the x, y, and degree to find the rotation points?)
I don't quiet understand the math for this one. The formula for rotation is:
x’ = x cos(Θ) – y sin(Θ)
y’ = x sin(Θ) + y cos(Θ)
Replies To: Question about math: keep track of a location in the Cartesian plane
#2
Re: Question about math: keep track of a location in the Cartesian plane
Posted 28 August 2013  08:50 PM
The second question is pretty straightforward. Get a piece of graph paper and plot a point, say (0,1), and move it by 2 along the X axis. Where does it end up? Now move it by 2 along the Y axis. Where does it end up? It's really that simple.
For the third question, yes, you'll want to write a method to apply the pair of formulas you've got. This is dead simple, since you have the formulas  just write those in Java. The Math class has methods to get the sine and cosine, so you're pretty much set.
When you've done that, you should review your trig and figure out why those formulas work. It's worth knowing.
#3
Re: Question about math: keep track of a location in the Cartesian plane
Posted 28 August 2013  09:44 PM
#4
Re: Question about math: keep track of a location in the Cartesian plane
Posted 28 August 2013  09:52 PM
jon.kiparsky, on 28 August 2013  08:50 PM, said:
But I created 2 methods, is it ok like this:
public double calcRotation() { double xPrime = (x*Math.cos(degree))(y*Math.sin(degree)); return xPrime; } public double calRotation2() { double yPrime = (x*Math.cos(degree))+(y*Math.sin(degree)); return yPrime; }
#5
Re: Question about math: keep track of a location in the Cartesian plane
Posted 28 August 2013  09:53 PM
// Translate test case 1 Point p = new Point (4,5); Point correctTranslation = new Point(9,5); if (!translate(p, 5, 0).equals(correctTranslation)) // assuming that translate takes both x and y parameters) System.out.println("Translate failed test case 1") ;
public double calcRotation() { double xPrime = (x*Math.cos(degree))(y*Math.sin(degree)); return xPrime; } public double calRotation2() { double yPrime = (x*Math.cos(degree))+(y*Math.sin(degree)); return yPrime; }
This looks like a correct translation of the equations into java.
Should it not be a parameter for the method?
Also, wouldn't it make more sense to have one method that returns a Point object?
EDIT: Wait, no. Look again. Not correct. I was looking at your coding practice, not looking closely enough at your math.
x’ = x cos(Θ) – y sin(Θ)
y’ = x sin(Θ) + y cos(Θ)
This post has been edited by jon.kiparsky: 28 August 2013  10:00 PM
#6
Re: Question about math: keep track of a location in the Cartesian plane
Posted 28 August 2013  10:07 PM
Also how can I put both of them in the one method? And for my degree, I declared it all the way on top along with x and y.
#7
Re: Question about math: keep track of a location in the Cartesian plane
Posted 28 August 2013  10:15 PM
public double rotation(double degree, boolean isX) { double result; if(isX) result = firstequation; else result = secondequation; return result; }
rotation(degree, true); //First equation //or rotation(degree, false); //Second equation
^Scratch that, look at jon.kiparsky's method.
This post has been edited by schutzzz: 28 August 2013  10:45 PM
#8
Re: Question about math: keep track of a location in the Cartesian plane
Posted 28 August 2013  10:20 PM
Try it like this:
public double rotation (Point p, double theta) { double xprime, yprime; // extract x and y from p, assign appropriate values to xprime and yprime return new Point (xprime, yprime); }
#9
Re: Question about math: keep track of a location in the Cartesian plane
Posted 28 August 2013  10:35 PM
jon.kiparsky, on 29 August 2013  05:20 AM, said:
public double rotation (Point p, double theta) { double xprime, yprime; // extract x and y from p, assign appropriate values to xprime and yprime return new Point (xprime, yprime); }
Shouldn't it be this? since you're returning a new point
public Point rotation(Point p, double theta) { }
#10
Re: Question about math: keep track of a location in the Cartesian plane
Posted 28 August 2013  10:37 PM
schutzzz, on 29 August 2013  12:35 AM, said:
jon.kiparsky, on 29 August 2013  05:20 AM, said:
public double rotation (Point p, double theta) { double xprime, yprime; // extract x and y from p, assign appropriate values to xprime and yprime return new Point (xprime, yprime); }
Shouldn't it be this? since you're returning a new point
public Point rotation(Point p, double theta) { }
You're right, good catch.
#11
Re: Question about math: keep track of a location in the Cartesian plane
Posted 28 August 2013  10:45 PM
@ jon: kinda confuse, let's me see if I understand you right.
public double rotation (Point p, double theta) { double xprime, yprime; // extract x and y from p, assign appropriate values to xprime and yprime xprime = (x*Math.cos(theta))(y*Math.sin(theta)); yprime = (x*Math.sin(theta))+(y*Math.cos(theta)); return new Point (xprime, yprime); }
So where is p coming to play?
#12
Re: Question about math: keep track of a location in the Cartesian plane
Posted 28 August 2013  10:52 PM
TechnoFiction, on 29 August 2013  05:45 AM, said:
public double rotation (Point p, double theta) { double xprime, yprime; // extract x and y from p, assign appropriate values to xprime and yprime xprime = (x*Math.cos(theta))(y*Math.sin(theta)); yprime = (x*Math.sin(theta))+(y*Math.cos(theta)); return new Point (xprime, yprime); }
Point is coming from a Point object. A point object will have an X and a Y.
Here's a snippet of a little point class I threw together:
public class Point { private double x; private double y; public double getX() { return x; } public double getY() { return y; } public Point(double x, double y) { this.x = x; this.y = y; } @Override public String toString() { return "(" + getX() + "," + getY() + ")"; } }
You create your point object. In this point class we have methods to retrieve values for our x point and y point.
public final Point rotation(Point p, double theta) { double xprime = (p.getX() * Math.cos(theta))  (p.getY() * Math.sin(theta)); double yprime = (p.getX() * Math.cos(theta)) + (p.getY() * Math.sin(theta)); return new Point(xprime, yprime); }
Use the reference name p to access the object to get an X and Y value. and you return the method to a new Point such as,
Point rotation1 = rotation(point, 30.5);
#13
Re: Question about math: keep track of a location in the Cartesian plane
Posted 28 August 2013  10:58 PM
TechnoFiction, on 29 August 2013  12:45 AM, said:
@ jon: kinda confuse, let's me see if I understand you right.
public double rotation (Point p, double theta) { double xprime, yprime; // extract x and y from p, assign appropriate values to xprime and yprime xprime = (x*Math.cos(theta))(y*Math.sin(theta)); yprime = (x*Math.sin(theta))+(y*Math.cos(theta)); return new Point (xprime, yprime); }
So where is p coming to play?
The point (no pun intended) is that you generally want to avoid global variables. This method is going to perform a translation of any point around the origin, not just one particular point that you happen to have stored coordinates for.
Methods should be general in their application, that way they're more useful.
#14
Re: Question about math: keep track of a location in the Cartesian plane
Posted 28 August 2013  11:00 PM
#15
Re: Question about math: keep track of a location in the Cartesian plane
Posted 28 August 2013  11:01 PM
schutzzz, on 29 August 2013  12:52 AM, said:
TechnoFiction, on 29 August 2013  05:45 AM, said:
public double rotation (Point p, double theta) { double xprime, yprime; // extract x and y from p, assign appropriate values to xprime and yprime xprime = (x*Math.cos(theta))(y*Math.sin(theta)); yprime = (x*Math.sin(theta))+(y*Math.cos(theta)); return new Point (xprime, yprime); }
Point is coming from a Point object. A point object will have an X and a Y.
Here's a snippet of a little point class I threw together:
You could always just use this one.
Source code here.
