Okay here's a java example:
Code:
public class Point
{
private int x;
private int y;
Point() // defaut constructor... sets x and y to zero by default.
{}
Point( int xx, int yy ) // constructor w/ 2 args. sets x and y to whatever you specify.
{
x = xx;
y = yy;
}
public void setX( int p )
{
x = p;
}
public void setY( int p )
{
y = p;
}
public int getX()
{
return x;
}
public int getY()
{
return y;
}
public int distanceBetween( Point p2 )
{
return ((x - p2.x)/(y - p2.y));
}
}
Now in your main function:
Code:
Point atHome = new Point(); // atHome is 0,0.
Point atWork = new Point(5,6); // at work is 5,6.
atWork.setX(3); // we've changed the X coord (which was 5) to 3.
atWork.setY(4); // now atWork is at 3,4.
// print out the distance between x and y. (0-3)/(0-4) in this case.
// I know it's not the correct distance, but it's just an example.
System.out.println("Distance between work and home is: " + atHome.distanceBetween(atWork));
// now prints out some more information usting getX() and getY()
System.out.println("Home is at " + atHome.getX() + ", " + atHome.getY());
System.out.println("Work is at " + atWork.getX() + ", " + atWork.getY());
As you can see, this example makes it easy to keep track of a coordinate system. Instead of having to keep track of all these variables, it makes it easy to deal with everything. If you had 500 points, it's much easier to keep track of everything. You can then write useful functions and every object of type point can use them.
Hope this helps,
Owen
<Edited by Owen on 12-07-2000 at 05:24 PM>
Bookmarks