
-----------------------------------
nelsonkkyy
Tue Oct 26, 2010 12:52 pm

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.

-----------------------------------
DanShadow
Tue Oct 26, 2010 1:20 pm

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

-----------------------------------
SNIPERDUDE
Tue Oct 26, 2010 1:28 pm

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.

-----------------------------------
nelsonkkyy
Tue Oct 26, 2010 3:39 pm

Re: Need help with x,y coordinate calculations
-----------------------------------
this is all i have

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

I don't know how to make an input for the points.

-----------------------------------
DemonWasp
Tue Oct 26, 2010 4:34 pm

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:
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.

-----------------------------------
nelsonkkyy
Wed Oct 27, 2010 1:38 pm

Re: Need help with x,y coordinate calculations
-----------------------------------
i came up with a new method
i used a java and a test class

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

class TestPoint
{
    public static void main (String

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.

-----------------------------------
DemonWasp
Wed Oct 27, 2010 3:31 pm

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):
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 ) );
[/code]

-----------------------------------
nelsonkkyy
Wed Oct 27, 2010 9:24 pm

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


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)));
    }
}



class TestPoint
{
    public static void main (String

-----------------------------------
DanShadow
Thu Oct 28, 2010 10:02 am

RE:Need help with x,y coordinate calculations
-----------------------------------

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:


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 ;)

-----------------------------------
TerranceN
Thu Oct 28, 2010 3:01 pm

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);

-----------------------------------
nelsonkkyy
Sat Oct 30, 2010 6:04 pm

Re: Need help with x,y coordinate calculations
-----------------------------------


class TestPoint
{
    public static void main (String
