Document Actions

Point.java

Geographic point object class

Click here to get the file

Size 3.3 kB - File type text/plain

File contents

// Name: Point.java
// Intent: geographic point object class
// Programmer: John P. Kavanagh  #2748 2405
// Date Modified: 30 April 2002
// Audit: Clifford Meyers (cmeyers@buffalo.edu)

public class Point {

	private String projection;
	private	double x;
	private double y;

	// The point constructor
	public Point(String x_value , String y_value) {
		
		projection = "Mercator";
		x = new Double(x_value.trim()).doubleValue();
		y = new Double(y_value.trim()).doubleValue();		
		
		System.out.println("Instantiated new point (" + x + "," + y + ")" );			
	}
	
	public Point(int x_value , int y_value) {
		
		projection = "Mercator";
		x = x_value;
		y = y_value;		
		
		System.out.println("Instantiated new point (" + x + "," + y + ")" );			
	}	
	
	// Rotate coordinates about cartesian plane axis
	public void Rotate(String turn){

		double arc;
		arc = new Double(turn.trim()).doubleValue();
		x = x * Math.cos(Math.toRadians(arc));
		y = y * Math.sin(Math.toRadians(arc));
		
		System.out.println("Point rotated to (" + x + "," + y + ")" );		
		return;					
	}
	
	
	// Scale each coordinate by given factor
	public void Scale(String factor_value){

		double factor;
		factor = new Double(factor_value.trim()).doubleValue();	
		x = factor * x;
		y = factor * y;

		System.out.println("Point scaled to (" + x + "," + y + ")" );
		return;		
	}

	// Move given point to another location
	public void Translate(String x_value , String y_value){
	
		x = new Double(x_value.trim()).doubleValue();
		y = new Double(y_value.trim()).doubleValue();	
		
		System.out.println("Point translated to (" + x + "," + y + ")" );	
		return;
	}
	
	// Get the coordinate values (14 March 2002)
	
	public double getXvalue(){
		return x;
	}
	
	public double getYvalue(){
		return y;
	}	
	
	public boolean IsEqual(Point matchPt){
		return (getXvalue() == matchPt.getXvalue()) && (getYvalue() == matchPt.getYvalue());
	}	
	
	// Set coordinate projection (14 March 2002)
	
	public void SetProjection(String proj){
	
		projection = proj;
		
		System.out.println("Set coordinate projection: " + projection );		
		return;
	}
	
	// Get coordinate projection (14 March 2002)
	
	public String GetProjection(){
		
		//System.out.println("Get coordinate projection: " + projection );
		return projection;	
	}
	
	// Find distance from this point to given point ( 14 March 2002)
	
	public double FindDistance(Point anotherPoint){
		
		double distance = 0;

		// for readability, set distance formula variables
		
		double x1 = x;
		double y1 = y;

		double x2 = anotherPoint.getXvalue();
		double y2 = anotherPoint.getYvalue(); 		
		
		if ( projection == anotherPoint.GetProjection() ){

			// If both points have the same projection,
			//  then solve distance formula
			
			distance = Math.sqrt(Math.pow((x2 - x1),2) + Math.pow((y2 - y1),2));
			
			//System.out.println("Find distance apart: " + distance);
			return distance;
		}
		else {

			// In the case that the projections vary, the distance calculation will fail.

			System.out.println("Different projection systems! - unable to measure distance.");
			return distance;
		}
	}
	
	// Remove point
	public void Destroy(){
		x = 0;
		y = 0;
		projection = "";
	}
	
}
www.flickr.com