Need help with x,y coordinate calculations
Author |
Message |
nelsonkkyy
|
Posted: Tue Oct 26, 2010 12:52 pm Post subject: Need help with x,y coordinate calculations |
|
|
I have to ceate a program that does:
i) calculate the distances pt1_pt2, pt2_pt3, pt3_pt4 and pt3_pt1 and display the results.
ii) check if pt1 is the same as point pt4.
iii) add point pt1 to pt4 and pt2 to pt3 and display the results.
iv) subtract pt2 from pt1 and display the result. |
|
|
|
|
![](images/spacer.gif) |
Sponsor Sponsor
![Sponsor Sponsor](templates/subSilver/images/ranks/stars_rank5.gif)
|
|
![](images/spacer.gif) |
DanShadow
![](http://compsci.ca/v3/uploads/user_avatars/12142970654c996e83e6997.png)
|
Posted: Tue Oct 26, 2010 1:20 pm Post subject: RE:Need help with x,y coordinate calculations |
|
|
So... whats the problem your having?
i) Formulate an equation to determine the length of a line segment (you should have been taught that in class)
ii) Do an 'if' check to determine if pt1 == pt4
iii) Add a println statement to display the results of adding pt1+pt4, and pt2+pt3
iv) Add a println statement to display the results of subtracting pt2 from pt1 |
|
|
|
|
![](images/spacer.gif) |
SNIPERDUDE
![](http://compsci.ca/v3/uploads/user_avatars/16932914504cbd0ad6ceaf1.jpg)
|
Posted: Tue Oct 26, 2010 1:28 pm Post subject: RE:Need help with x,y coordinate calculations |
|
|
I don't know java too well (if there's a function already made for distance calculations, I imagine there would be somewhere), but if not Pythagorean's theorem is your friend. |
|
|
|
|
![](images/spacer.gif) |
nelsonkkyy
|
Posted: Tue Oct 26, 2010 3:39 pm Post subject: Re: Need help with x,y coordinate calculations |
|
|
this is all i have
Quote:
public class Point
{
private double x;
private double y;
private double distance;
public Point ()
{
x = 0.0;
y = 0.0;
}
public Point (double x, double y)
{
this.x = x;
this.y = y;
}
public double distanceTo (Point otherPoint)
{
double distance;
distance = Math.sqrt ((otherPoint.x) * (x) + (otherPoint.y) * (x));
return distance;
}
public double getDistance ()
{
return distance;
}
public String toString ()
{
return "The distance from Point to otherPoint is" + distance + ".";
}
public static void main (String[] args)
{
Point p0 = new Point ();
Point p1 = new Point (0.0, 0.0);
Point p2 = new Point (1.0, 1.0);
Point p3 = new Point (10.0, 1.0);
System.out.println (p0.toString ());
System.out.println (p1.toString ());
System.out.println (p2.toString ());
System.out.println (p3.toString ());
System.out.println ("The distance from p1 to p2 is " + p1.distanceTo (p2));
System.out.println ("The distance from p1 to p3 is " + p1.distanceTo (p3));
} // end of method main
}
I don't know how to make an input for the points. |
|
|
|
|
![](images/spacer.gif) |
DemonWasp
|
Posted: Tue Oct 26, 2010 4:34 pm Post subject: RE:Need help with x,y coordinate calculations |
|
|
First, distance isn't a property of a single point, it's a property of the relationship between two points. Storing distance in each point object makes no sense, so remove that instance variable.
Second, your distanceTo(Point) method is incorrect. Look carefully at it and compare it to the method for Pythagorean theorem.
Third, you should write a function that looks like this:
code: |
public boolean equalsPoint ( Point other ) {
// Fill in an equality method here. This should be easy.
// Two points are equal if and only if both X and Y coordinates are the same.
}
|
Fourth, write an add ( Point other ) method. This should be simple - add the (x,y) coordinates of other to the (x,y) coordinates of this.
Fifth, write a subtract ( Point other ) method. This is even simpler, since it's basically the same thing as the add method from step 4.
Finally, use all those utility methods in your main() method to do what your assignment requires. |
|
|
|
|
![](images/spacer.gif) |
nelsonkkyy
|
Posted: Wed Oct 27, 2010 1:38 pm Post subject: Re: Need help with x,y coordinate calculations |
|
|
i came up with a new method
i used a java and a test class
Quote:
class Point
{
int x;
int y;
void setx (int x)
{
this.x = x;
}
void sety (int y)
{
this.y = y;
}
int getx ()
{
return this.x;
}
int gety ()
{
return this.y;
}
void add (Point p)
{
x = x + p.x;
y = y + p.y;
}
double dist (Point p)
{
return Math.sqrt ((Math.pow ((x - p.x), 2) + Math.pow ((y - p.y), 2)));
}
}
and
Quote:
class TestPoint
{
public static void main (String[] args)
{
Point p1 = new Point ();
Point p2 = new Point ();
Point p3 = new Point ();
Point p4 = new Point ();
p1.setx (10);
p1.sety (20);
p2.setx (11);
p2.sety (21);
p3.setx (1);
p3.sety (2);
p4.setx (4);
p4.sety (8);
System.out.println (p1.x);
System.out.println (p1.y);
System.out.println (p2.x);
System.out.println (p2.y);
System.out.println (p3.x);
System.out.println (p3.y);
System.out.println (p4.x);
System.out.println (p4.y);
p1.add (p2);
System.out.println (p1.y);
p1.dist (p2);
}
}
how do i make to display as one point like(x,y) but not x and then y. It does not calculate the distance with a x,y coordinate. |
|
|
|
|
![](images/spacer.gif) |
DemonWasp
|
Posted: Wed Oct 27, 2010 3:31 pm Post subject: RE:Need help with x,y coordinate calculations |
|
|
First, if you want to put multiple things on the same line, you can either perform multiple prints without printing a newline (print, not println):
code: |
System.out.print ( "(" );
System.out.print ( p1.getX() );
System.out.print ( ", " );
System.out.print ( p2.getY() );
System.out.println ( ")" );
|
Or you can concatenate variables:
code: |
System.out.println ( "(" + p1.getX() + ", " + p1.getY() + ")" );
|
Or you can do it the really clever way, like a real Java programmer would. First, override the toString() method in your Point class:
code: |
// ... put this inside the Point class
public String toString() {
return "("+x+", "+y+")";
}
|
Once you have that, you can send Point objects to println() and they will call this method to turn themselves into a String representation that is then printed:
code: |
System.out.println ( p1 );
|
This last suggestion is by far the best, since it is then very simple to output as many points as you want, or arrays or Lists of points.
code: |
List<Point> myPoints = // (list assembled somehow)
System.out.println ( myPoints );
|
Second, you should consider renaming setx, sety, getx, gety to setX, setY, getX, getY, as those correspond to the standard Java naming conventions. Good IDEs will help you along here by letting you refactor your code, but if you're using RTP you're out of luck (good luck manually editing all that).
Third, it does calculate the distance, it just does nothing with that information. If you want to do something, you should print the result of the dist(Point) call:
code: |
System.out.println ( "Distance between p1 and p2 is: " + p1.dist ( p2 ) );
|
|
|
|
|
|
![](images/spacer.gif) |
nelsonkkyy
|
Posted: Wed Oct 27, 2010 9:24 pm Post subject: Re: Need help with x,y coordinate calculations |
|
|
I got most of it done, but i still dont know how to add from p1 to p4 and subtract form p2 to p1
Quote:
class Point
{
int x;
int y;
void setx (int x)
{
this.x = x;
}
void sety (int y)
{
this.y = y;
}
int getx ()
{
return this.x;
}
int gety ()
{
return this.y;
}
void add (Point p)
{
x = x + p.x;
y = y + p.y;
}
void subtract (Point p)
{
x = x - p.x;
y = y - p.y;
}
double dist (Point p)
{
return Math.sqrt ((Math.pow ((x - p.x), 2) + Math.pow ((y - p.y), 2)));
}
}
Quote:
class TestPoint
{
public static void main (String[] args)
{
Point p1 = new Point ();
Point p2 = new Point ();
Point p3 = new Point ();
Point p4 = new Point ();
p1.setx (-2);
p1.sety (1);
p2.setx (1);
p2.sety (5);
p3.setx (1);
p3.sety (2);
p4.setx (-2);
p4.sety (1);
System.out.println ( "Point1(" + p1.getx() + ", " + p1.gety() + ")" );
System.out.println ( "Point2(" + p2.getx() + ", " + p2.gety() + ")" );
System.out.println ( "Point3(" + p3.getx() + ", " + p3.gety() + ")" );
System.out.println ( "Point4(" + p4.getx() + ", " + p4.gety() + ")" );
p2.add (p3);
System.out.println ( "Addition Point2 to point3(" + p2.getx() + ", " + p2.gety() + ")" );
System.out.println ( "Addition Point1 to point4(" + p1.getx() + ", " + p1.gety() + ")" );
p1.subtract (p2);
System.out.println ( "Subtraction Point2 to point1(" + p1.getx() + ", " + p1.gety() + ")" );
p1.dist (p2);
System.out.println ( "Distance between p1 and p2 is: " + p1.dist ( p2 ) );
System.out.println ( "Distance between p2 and p3 is: " + p2.dist ( p3 ) );
System.out.println ( "Distance between p3 and p4 is: " + p3.dist ( p4 ) );
System.out.println ( "Distance between p3 and p1 is: " + p3.dist ( p1 ) );
}
}
|
|
|
|
|
![](images/spacer.gif) |
Sponsor Sponsor
![Sponsor Sponsor](templates/subSilver/images/ranks/stars_rank5.gif)
|
|
![](images/spacer.gif) |
DanShadow
![](http://compsci.ca/v3/uploads/user_avatars/12142970654c996e83e6997.png)
|
Posted: Thu Oct 28, 2010 10:02 am Post subject: RE:Need help with x,y coordinate calculations |
|
|
Assignment Instructions wrote:
iii) add point pt1 to pt4 and pt2 to pt3 and display the results.
iv) subtract pt2 from pt1 and display the result.
Well, the assignment doesnt seem to specify that your addition/subtraction 'must' take place as a function call (which is easy enough, but seemingly unnecessary).
Personally, I would do something like this:
Code wrote:
System.out.println("p4 + p1 = (x, y)");
System.out.println("("+ (p4.getx() + p1.getx() +", "+ (p4.gety() + p1.gety() +")");
System.out.println("p1 - p2 = (x, y)");
System.out.println("("+ (p1.getx() - p2.getx() +", "+ (p1.gety() - p2.gety() +")");
Hope this helps.
P.S. Dont forget comments ![Wink Wink](http://compsci.ca/v3/images/smiles/icon_wink.gif) |
|
|
|
|
![](images/spacer.gif) |
TerranceN
|
Posted: Thu Oct 28, 2010 3:01 pm Post subject: RE:Need help with x,y coordinate calculations |
|
|
First, methods are private by default, meaning that only other methods in that class are able to call them. You need to use the keyword public to make other classes be able to call them.
Next, a constructor would really help out with cleaning up the stuff like p2.setx(1);
To create a constructor, create a public instance method whose name is the same as the class, for example;
public Point()
{
// do something relevant to creating the point here, such as setting x and y to be 0;
}
and it can be used like this
Point p = new Point();
You can add parameters to the method, so you can send the initial x and y values to the constructor instead of doing everything individually. So it would be
Point p = new Point(5, 7);
Also, a really helpful thing about classes is that a method can return the same type as the class represents. This can be used to get p1 + p2 without changing either one. You would be able to type something like this:
Point Point2PlusPoint3 = p2.plus(p3); // where plus is the method that you created that returns a Point
System.out.println ( "Addition Point2 to point3(" + Point2PlusPoint3.getx() + ", " + Point2PlusPoint3.gety() + ")" );
Currently in your code when you do p2.add(p3);, it changes the value of p2, so when you do p1.subtract(p2);, it is really (in terms of how the variables started) p1 - (p2 + p3); |
|
|
|
|
![](images/spacer.gif) |
nelsonkkyy
|
Posted: Sat Oct 30, 2010 6:04 pm Post subject: Re: Need help with x,y coordinate calculations |
|
|
Quote:
class TestPoint
{
public static void main (String[] args)
{
Point p1 = new Point ();
Point p2 = new Point ();
Point p3 = new Point ();
Point p4 = new Point ();
p1.setx (2);
p1.sety (4);
p2.setx (3);
p2.sety (5);
p3.setx (9);
p3.sety (7);
p4.setx (6);
p4.sety (2);
System.out.println ("Point1(" + p1.getx () + ", " + p1.gety () + ")");
System.out.println ("Point2(" + p2.getx () + ", " + p2.gety () + ")");
System.out.println ("Point3(" + p3.getx () + ", " + p3.gety () + ")");
System.out.println ("Point4(" + p4.getx () + ", " + p4.gety () + ")");
System.out.println (" ");
p2.add (p3);
System.out.println ("p2 + p3 = (x, y)");
System.out.println ("(" + p2.getx () + ", " + p2.gety () + ")");
System.out.println (" ");
System.out.println ("p4 + p1 = (x, y)");
System.out.println ("(" + (p4.getx () + p1.getx ()) + ", " + (p4.gety () + p1.gety ()) + ")");
System.out.println (" ");
p2.subtract (p1);
System.out.println ("p2 - p1 = (x, y)");
System.out.println ("("+ (p2.getx() - p1.getx()) +", "+ (p2.gety() - p1.gety()) +")");
System.out.println (" ");
p1.dist (p2);
System.out.println ("Distance between p1 and p2 is: " + p1.dist (p2));
System.out.println ("Distance between p2 and p3 is: " + p2.dist (p3));
System.out.println ("Distance between p3 and p4 is: " + p3.dist (p4));
System.out.println ("Distance between p3 and p1 is: " + p3.dist (p1));
}
}
I cant get the subtraction, beacuse im over wrtting the point.
Also i have to make the inputs for the coordinates, cuz every time i have to make changes from the code. |
|
|
|
|
![](images/spacer.gif) |
|
|