Question about math: keep track of a location in the Cartesian plane

  • (2 Pages)
  • +
  • 1
  • 2

29 Replies - 939 Views - Last Post: 30 August 2013 - 11:21 PM Rate Topic: -----

#1 TechnoFiction  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 68
  • Joined: 21-September 12

Question about math: keep track of a location in the Cartesian plane

Posted 28 August 2013 - 08:32 PM

How do I:
-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(Θ)

Is This A Good Question/Topic? 0
  • +

Replies To: Question about math: keep track of a location in the Cartesian plane

#2 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 7747
  • View blog
  • Posts: 13,106
  • Joined: 19-March 11

Re: Question about math: keep track of a location in the Cartesian plane

Posted 28 August 2013 - 08:50 PM

The answer to the first question will depend on how you're representing the plane and the points on it. Perhaps you have a Plane class, which would serve as a collection of Points and present some useful methods for working with those points, or maybe you're working for Point objects, or who knows. We'll need more information for that one.

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.
Was This Post Helpful? 1
  • +
  • -

#3 TechnoFiction  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 68
  • Joined: 21-September 12

Re: Question about math: keep track of a location in the Cartesian plane

Posted 28 August 2013 - 09:44 PM

Thanks so much for the answers. I think I already had most of the stuff in my code. Would love for you to check it, but with turnitin around, it's kinda hard to post freely anymore.
Was This Post Helpful? 0
  • +
  • -

#4 TechnoFiction  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 68
  • Joined: 21-September 12

Re: Question about math: keep track of a location in the Cartesian plane

Posted 28 August 2013 - 09:52 PM

Just have one last question for you, you said:

View Postjon.kiparsky, on 28 August 2013 - 08:50 PM, said:

For the third question, yes, you'll want to write a method to apply the pair of formulas you've got.


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;
	}

Was This Post Helpful? 0
  • +
  • -

#5 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 7747
  • View blog
  • Posts: 13,106
  • Joined: 19-March 11

Re: Question about math: keep track of a location in the Cartesian plane

Posted 28 August 2013 - 09:53 PM

Testing your code is an important skill. If you know what it's supposed to do, you can write tests to see if it does that. For example, if you know that translating (4,5) by 5 along the X axis should get you a certain point, you can test to see if it does that. Coming up with the cases that really test the code is a great way to find out if you really understand the problem you're trying to solve. It can also give you insights about how your code should be written. There's a lot out there on this - useful search terms include "test-driven development" and "unit testing". Junit is a commonly-used tool for testing Java, but you can start by writing classes that just do things like

// 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.
But where does the degree variable come from?
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

Was This Post Helpful? 0
  • +
  • -

#6 TechnoFiction  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 68
  • Joined: 21-September 12

Re: Question about math: keep track of a location in the Cartesian plane

Posted 28 August 2013 - 10:07 PM

Oh yeah, sorry! I thought they both the same and copy/paste them. I remembered something wrong with it, gonna change it now.

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.
Was This Post Helpful? 0
  • +
  • -

#7 schutzzz  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 136
  • View blog
  • Posts: 338
  • Joined: 22-April 13

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;
}



with a usage of
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

Was This Post Helpful? 0
  • +
  • -

#8 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 7747
  • View blog
  • Posts: 13,106
  • Joined: 19-March 11

Re: Question about math: keep track of a location in the Cartesian plane

Posted 28 August 2013 - 10:20 PM

Okay, now you're just going to confuse the poor fellow.

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);
}

Was This Post Helpful? 0
  • +
  • -

#9 schutzzz  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 136
  • View blog
  • Posts: 338
  • Joined: 22-April 13

Re: Question about math: keep track of a location in the Cartesian plane

Posted 28 August 2013 - 10:35 PM

View Postjon.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) { }


Was This Post Helpful? 1
  • +
  • -

#10 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 7747
  • View blog
  • Posts: 13,106
  • Joined: 19-March 11

Re: Question about math: keep track of a location in the Cartesian plane

Posted 28 August 2013 - 10:37 PM

View Postschutzzz, on 29 August 2013 - 12:35 AM, said:

View Postjon.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.
Was This Post Helpful? 0
  • +
  • -

#11 TechnoFiction  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 68
  • Joined: 21-September 12

Re: Question about math: keep track of a location in the Cartesian plane

Posted 28 August 2013 - 10:45 PM

@ schutzzz: Oh, dang...there's that way to put them in one method, thanks for reminding me. But that's not exactly what I'm looking for. Those 2 equation are like x and y, I can't do one or the other, they both must be present at the same time to rotate it. I remember did something similar in matlab a few months back.

@ 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?
Was This Post Helpful? 0
  • +
  • -

#12 schutzzz  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 136
  • View blog
  • Posts: 338
  • Joined: 22-April 13

Re: Question about math: keep track of a location in the Cartesian plane

Posted 28 August 2013 - 10:52 PM

View PostTechnoFiction, 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);


Was This Post Helpful? 1
  • +
  • -

#13 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 7747
  • View blog
  • Posts: 13,106
  • Joined: 19-March 11

Re: Question about math: keep track of a location in the Cartesian plane

Posted 28 August 2013 - 10:58 PM

View PostTechnoFiction, on 29 August 2013 - 12:45 AM, said:

@ schutzzz: Oh, dang...there's that way to put them in one method, thanks for reminding me. But that's not exactly what I'm looking for. Those 2 equation are like x and y, I can't do one or the other, they both must be present at the same time to rotate it. I remember did something similar in matlab a few months back.

@ 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.
Was This Post Helpful? 1
  • +
  • -

#14 TechnoFiction  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 68
  • Joined: 21-September 12

Re: Question about math: keep track of a location in the Cartesian plane

Posted 28 August 2013 - 11:00 PM

Oh, I see now. Thanks so much.
Was This Post Helpful? 0
  • +
  • -

#15 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 7747
  • View blog
  • Posts: 13,106
  • Joined: 19-March 11

Re: Question about math: keep track of a location in the Cartesian plane

Posted 28 August 2013 - 11:01 PM

View Postschutzzz, on 29 August 2013 - 12:52 AM, said:

View PostTechnoFiction, 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.
Was This Post Helpful? 1
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2