/* * vec2d.java * * Created on March 23, 2007, 4:20 AM * * To change this template, choose Tools | Template Manager * and open the template in the editor. */ /** vec2d is a class representing the mathematical concept of two-dimensional vector. In perticular we can consider a vec2d object to be a point in two dimensional space. This class includes the following operations:

This class is written for the Java tutorials of SE2AA4 (Winter 07). @author Sui Huang */ public class vec2d { private static double _tolerance=0.00000001; private double _x, _y; /** Creates a new instance of vec2d @param x the first coordinate of the vector @param y the second coordinate of the vector */ public vec2d(double x, double y) { _x = x; _y = y; } /** * clone another copy of itself * this function makes sure the new copy have its own copy of data in memory * other than those data of the original vector. *@return an independent copy of the object who call this function. */ public vec2d clone(){ return new vec2d(this.getX(), this.getY()); } /** Get the first coordinate of the vec2d object. @return the value of first coordinate. */ public double getX(){ return _x; } /** Get the second coordinate of the vec2d object. @return the value of second coordinate. */ public double getY(){ return _y; } /** Calculate the 2-norm of the vector, which is the square root of the sum of the square of the coordinates. In geometry 2-norm is usually considered the magnitude or the length of a vector. @return 2-norm of vector object. */ public double getLength(){ return java.lang.Math.hypot(_x,_y); } /** Scale the vector to a unit vector. Unit vector is vector of length 1, which means the sum of squares of the first and the second coordinates is one. If the vector is (0,0), this function will throw the java.lang.IllegalStateException. */ public void normalize() throws java.lang.IllegalStateException{ double len = this.getLength(); if (len==0) { throw new java.lang.IllegalStateException("normalize zero vector"); } _x = _x/len; _y = _y/len; } /** Compute the sum of two vectors. *@param v1 the first vector to be added *@param v2 the second vector to be added *@return the coordinate-wise sum of v1 and v2 */ public static vec2d add(vec2d v1, vec2d v2){ return new vec2d(v1.getX()+v2.getX(), v1.getY()+v2.getY()); } /** scale a vector. *This function will scale an input vector to the input ratio of length, *without changing the vector's direction. *@param r the ratio to scale the vector *@param v the vector to be scaled *@return the vector obtain by scaling */ public static vec2d mul(double r, vec2d v){ return new vec2d(r*v.getX(), r*v.getY()); } /** check if three points are in the same line of two dimensional space. *@param v1 the vector representing the first point *@param v2 the vector representing the second point *@param v3 the vector representing the third point *@return true if the three points are at the same line, false otherwise */ public static boolean coline(vec2d v1, vec2d v2, vec2d v3){ double slope1, slope2; /* * Bug: * There will be division by zero if v1 and v3 have identical * x-coordinate, or v2 and v3. * Difficulty: * This is an easy bug to find. */ slope1 = (v1.getY()-v3.getY())/(v1.getX()-v3.getX()); slope2 = (v2.getY()-v3.getY())/(v2.getX()-v3.getX()); if (Math.abs(Math.abs(slope1)-Math.abs(slope2))<_tolerance){ return true; } return false; } /** * return the projection of vector u on vector v * For two vectors u and v in linear algebra, the projection of u on v is * the component of u such that in the same direction of v. If w is the * projection of u on v, then the dot product of (u-w) and v is zero. * A special case is that if v=(0,0), then the projection of u on v is * (0,0). *@param u the vector that we want to find the component by projecting onto * the other *@param v the vector on which u is projected *@return the project of u on v */ public static vec2d proj(vec2d u, vec2d v){ double x, y; vec2d normed_v; double dot; if ((v.getX()==0)&&(v.getY()==0)){ x = 0; y = 0; return new vec2d(x,y); } normed_v = new vec2d(v.getX(),v.getY()); normed_v.normalize(); dot = normed_v.getX()*u.getX()+normed_v.getY()*u.getY(); return vec2d.mul(dot, normed_v); } /** * Computer the area of a triangle. * This routine will return the area of triangle having the three input * vectors as vertices. *@param v1 the first vertex *@param v2 the second vertex *@param v3 the third vertex *@return the area of the triangle generated by v1, v2 and v3 */ public static double triangleArea(vec2d v1, vec2d v2, vec2d v3){ /* * Bug: * use an incorrect formula to compute the area, such that the * returned result is twice of the correct area. * Difficulty: * Easy to find. */ vec2d u = new vec2d(v1.getX()-v3.getX(), v1.getY()-v3.getY()); vec2d v = new vec2d(v2.getX()-v3.getX(), v2.getY()-v3.getY()); vec2d w = vec2d.add(u, vec2d.mul(-1,vec2d.proj(u,v))); return Math.abs(v.getLength()*w.getLength()); } }