/*
 * PlainPoint.java
 *
 * Created on December 11, 2001, 12:29 AM
 * For further information please read Konvneck.txt
 */


/**
 * The implementation of a plain 2D point
 * @author  Peter Klett e0125739@student.tuwien.ac.at
 * @version 0.1
 */
public class PlainPoint 
{
    /**The x-value of this point*/
    private float x;
    /**The y-value of this point*/
    private float y;
    /**The minimum for a coodinate*/
    private static final int MIN = -100;  
    /**The maximum for a coodinate*/
    private static final int MAX = 100;
    
    
    /**
     * Creates a new PlainPoint from a given String
     * @param coodinates a String in the format "(x,y)"
     * @throws IllegalArgumentException if the String is of wrong format
     */
    public static PlainPoint createPoint(String coordinates) throws IllegalArgumentException
    {
        PlainPoint ret = new PlainPoint();
        int commaIndex = -1;
        
        if (coordinates == null)
            throw new IllegalArgumentException("Input is null");
        try
        {
            if (coordinates.charAt(0) != '(' || coordinates.charAt(coordinates.length()-1) != ')')
                throw new IllegalArgumentException("Wrong input data");
            
            commaIndex = coordinates.indexOf(",");  
            
            ret.setX(Float.parseFloat(coordinates.substring(1,commaIndex)));
            ret.setY(Float.parseFloat(coordinates.substring(commaIndex+1, coordinates.length()-1)));

        }
        catch (Exception x)
        {
            throw new IllegalArgumentException(x.toString());
        }
        return ret;
    } 
    
    
    /** Creates new PlainPoint at (0|0)*/
    public PlainPoint() 
    {
        x = 0;
        y = 0;
    }
    
    /**Creates new PlainPoint at the given coordinates*/
    public PlainPoint(float x, float y)
    {
        this.x = x;
        this.y = y;
    }
    
    /**the 'normal' get- and set-functions*/
    public void setX(float x)
    {
        this.x = x;
    }
    
    public void setY(float y)
    {
        this.y = y;
    }
    
    public float getX()
    {
        return x;
    }
    
    public float getY()
    {
        return y;
    }
    
    /**Checks if this point is valid, means between given borders*/
    public boolean validate()
    {
        if (x < MIN || x > MAX)
            return false;
        if (y < MIN || y > MAX)
            return false;
        return true;
    }
    
    /**Checks if this point is equals to a given object*/
    public boolean equals(Object o)
    {
        if (o instanceof PlainPoint)
        {
            PlainPoint tempPoint = (PlainPoint)o;
            if (this.x == tempPoint.getX())
            {
                if (this.y == tempPoint.getY())
                {
                    return true;
                }
            }
            
        }
        return false;
    }
    
    /**needed for testing*/
    public String toString()
    {
        return "("+x+"|"+y+")";
    }
        

}