/*  Class Point
*
*   Defines a point in multidimensional space
*   Several methods using Points are only valid for 1, 2 or 3-dimensional space
* 
*   Required by the class Vector
*    
*   WRITTEN BY: Dr Michael Thomas Flanagan
*
*   DATE:	November - December 2011
*   UPDATES:    5 December 2011
*
*   DOCUMENTATION:
*   See Michael Thomas Flanagan's Java library on-line web page:
*   http://www.ee.ucl.ac.uk/~mflanaga/java/Point.html
*   http://www.ee.ucl.ac.uk/~mflanaga/java/
*
*   Copyright (c) 2011  Michael Thomas Flanagan
*   PERMISSION TO COPY:
*
* Permission to use, copy and modify this software and its documentation for NON-COMMERCIAL purposes is granted, without fee,
* provided that an acknowledgement to the author, Dr Michael Thomas Flanagan at www.ee.ucl.ac.uk/~mflanaga, appears in all copies
* and associated documentation or publications.
*
* Redistributions of the source code of this source code, or parts of the source codes, must retain the above copyright notice, this list of conditions
* and the following disclaimer and requires written permission from the Michael Thomas Flanagan:
*
* Redistribution in binary form of all or parts of this class must reproduce the above copyright notice, this list of conditions and
* the following disclaimer in the documentation and/or other materials provided with the distribution and requires written permission from the Michael Thomas Flanagan:
*
* Dr Michael Thomas Flanagan makes no representations about the suitability or fitness of the software for any or for a particular purpose.
* Dr Michael Thomas Flanagan shall not be liable for any damages suffered as a result of using, modifying or distributing this software
* or its derivatives.
*
***************************************************************************************/

package flanagan.math;

import java.math.*;

public class Point{
    
    private double[] point = null;          // cartesian coordinates
    private int nDimensions = 0;            // number of dimensions  
    
    // CONSTUCTORS
    // 3D zero point
    public Point(){
        this.point = new double[3];
        this.point[0] = 0.0;
        this.point[1] = 0.0;
        this.point[2] = 0.0;
        this.nDimensions = 3;
    }
    
    // All coordinates supplid as an array of doubles
    public Point(double[] coordinates){
        this.setPoint(coordinates);
    }
    
    // All coordinates supplid as an array of float
    public Point(float[] coordinates){
        this.setPoint(coordinates);
    }
    
    // All coordinates supplid as an array of long
    public Point(long[] coordinates){
        this.setPoint(coordinates);
    }
    
        // All coordinates supplid as an array of int
    public Point(int[] coordinates){
        this.setPoint(coordinates);
    }
    
        // All coordinates supplid as an array of short
    public Point(short[] coordinates){
        this.setPoint(coordinates);
    }
    
    // All coordinates supplid as an array of BigDecimal
    public Point(BigDecimal[] coordinates){
        this.setPoint(coordinates);
    }
    
    // All coordinates supplid as an array of BigInteger
    public Point(BigInteger[] coordinates){
        this.setPoint(coordinates);
    }
    
    // 1D point
    public Point(double xPoint){
        this.point = new double[1];
        this.point[0] = xPoint;
        this.nDimensions = 1;
    }
    
    // 2D point
    public Point(double xPoint, double yPoint){
        this.point = new double[2];
        this.point[0] = xPoint;
        this.point[1] = yPoint;
        this.nDimensions = 2;
    }  

    // 3D point
    public Point(double xPoint, double yPoint, double zPoint){
        this.point = new double[3];
        this.point[0] = xPoint;
        this.point[1] = yPoint;
        this.point[2] = zPoint;
        this.nDimensions = 3;
    }   
    
    // RESET COORDINATES
    // All coordinates supplied as an array of doubles
    public final void setPoint(double[] coordinates){
        this.point = (double[])coordinates.clone(); 
        this.nDimensions = this.point.length;
    }
    
    // All coordinates supplied as an array of float
    public final void setPoint(float[] coordinates){
        this.nDimensions = coordinates.length;
        ArrayMaths am = new ArrayMaths(coordinates);
        this.point = am.array_as_double(); 
    }
    
    // All coordinates supplied as an array of long
    public final void setPoint(long[] coordinates){
        this.nDimensions = coordinates.length;
        ArrayMaths am = new ArrayMaths(coordinates);
        this.point = am.array_as_double(); 
    }
    
    // All coordinates supplied as an array of int
    public final void setPoint(int[] coordinates){
        this.nDimensions = coordinates.length;
        ArrayMaths am = new ArrayMaths(coordinates);
        this.point = am.array_as_double(); 
    }
    
    // All coordinates supplied as an array of short
    public final void setPoint(short[] coordinates){
        this.nDimensions = coordinates.length;
        ArrayMaths am = new ArrayMaths(coordinates);
        this.point = am.array_as_double(); 
    }
    
    // All coordinates supplied as an array of BigDecimal
    public final void setPoint(BigDecimal[] coordinates){
        this.nDimensions = coordinates.length;
        ArrayMaths am = new ArrayMaths(coordinates);
        this.point = am.array_as_double(); 
    }
    
    // All coordinates supplied as an array of Big Integer
    public final void setPoint(BigInteger[] coordinates){
        this.nDimensions = coordinates.length;
        ArrayMaths am = new ArrayMaths(coordinates);
        this.point = am.array_as_double(); 
    }
    
    // 1D point
    public void setPoint(double xPoint){
        this.point = new double[1];
        this.point[0] = xPoint;
        this.nDimensions = 1;
    }
    
    // 2D point
    public void setPoint(double xPoint, double yPoint){
        this.point = new double[2];
        this.point[0] = xPoint;
        this.point[1] = yPoint;
        this.nDimensions = 2;
    }  

    // 3D point
    public void setPoint(double xPoint, double yPoint, double zPoint){
        this.point = new double[3];
        this.point[0] = xPoint;
        this.point[1] = yPoint;
        this.point[2] = zPoint;
        this.nDimensions = 3;
    }   
    
    // ONE D ARRAY OF n POINTS 
    // all points are 3D zero points 
    public static Point[] oneDarray(int n){
        Point[] pa = new Point[n];
        for(int i=0; i3)System.out.println("Methods distance and distanceSquared are only meaningful for dimensions of 3 or less");
        double hold = 0.0;
        double sum = 0.0;
        for(int i=0; i3)System.out.println("Methods distance and distanceSquared are only meaningful for dimensions of 3 or less");
        double hold = 0.0;
        double sum = 0.0;
        for(int i=0; i3){
            System.out.println("Method toThreeD:  Dimensions are greater than three so instance cannot be convertd to a three D Point");
        }
        else{
            if(this.nDimensions<3){
                for(int i=0; i3)throw new IllegalArgumentException("VectorMaths is restricted to 2 or 3-dimensional space");
        VectorMaths vec = new VectorMaths(this.point);
        return vec;
    }
}

 

Ads help maintain this website.