next up previous contents
Next: Conclusions Up: MyUtil Package Previous: UNNL Class

xPoint Class

The xPoint class is based on a suggestion Robert Simms [10] made after discussing some problems with developing code to draw vertices and edges. This class extends the Java Point class and provides a variety of methods which allow xPoint objects to be used somewhat like two dimensional vectors. The Edge and Vertex classes use xPoint objects extensively in drawing. The xPoint class was particularly useful in adding arrows to directed edges.

package MyUtil;

import java.awt.Point;
import java.util.*;

public class xPoint extends java.awt.Point implements Cloneable {

   public xPoint (int x, int y) {
      super(x, y);
   }

   public xPoint (xPoint p) {
      super(p.x, p.y);
   }

   public Object clone() {
        return new xPoint(x,y);
   }

   public String toString() {
        return "("+x+","+y+")";
   }

   public static xPoint fromString(String string) {
        return fromString(string,0);
   }

   public static xPoint fromString(String string, int first) {
         int t_x=0, t_y=0;
         StringTokenizer st 
              = new StringTokenizer(string.substring(first),"(,)");
         try {  
            t_x=Integer.parseInt(st.nextToken());
            t_y=Integer.parseInt(st.nextToken());
            return new xPoint(t_x,t_y);
         }
         catch (NumberFormatException e) {
            System.out.println(
               "Error: xPoint.fromString() - NumberFormatException"); 
         }
         catch (NoSuchElementException e) {
            System.out.println("Error: xPoint.fromString() - Missing Data"); 
         }
         return null;

   }   

   public xPoint add(int x, int y) {
      return new xPoint(this.x + x,this.y + y);
   }

   public xPoint add(xPoint p) {
      return new xPoint(this.x + p.x,this.y + p.y);
   }

   public xPoint sub(int x , int y) {
      return new xPoint(this.x - x,this.y - y);
   }

   public xPoint sub(xPoint p) {
      return new xPoint(this.x - p.x,this.y - p.y);
   }

   public xPoint moveDirection(int r, xPoint p) {
   // returns the xPoint r units in the xPoint p DIRECTION
      int xshift,yshift;
      double len = Math.sqrt(p.x*p.x+p.y*p.y);
      xshift = (int)((double)(r*(p.x))/len);
      yshift = (int)((double)(r*(p.y))/len);
      return new xPoint(this.x+xshift,this.y+yshift);
   }



   public xPoint moveToward(int r, xPoint p) {
   // returns the xPoint r units toward xPoint p
      if (p.x==x && p.y==y)
         return ((xPoint)this.clone());
      int xshift,yshift;
      xshift = r*(p.x-this.x)/this.dist(p);
      yshift = r*(p.y-this.y)/this.dist(p);
      return new xPoint(this.x+xshift,this.y+yshift);
   }

   public xPoint moveRorLP(int r, xPoint p, int mult) {
      int xdif=p.x-this.x;
      int ydif=p.y-this.y;
      if (xdif==0)
         return new xPoint(this.x+(p.y<this.y?mult*r:-mult*r),this.y);
      if (ydif==0)
         return new xPoint(this.x,this.y+(p.x<this.x?-mult*r:mult*r));
      return this.moveDirection(r,new xPoint(-ydif*mult,xdif*mult));
   }

   public xPoint moveLP(int r, xPoint p) {
      return moveRorLP(r,p,-1);
   }

   public xPoint moveRP(int r, xPoint p) {
      return moveRorLP(r,p,1);
   }

   public void max(xPoint p) {
      if (this.x < p.x) this.x = p.x;
      if (this.y < p.y) this.y = p.y;   
   }

   public void min(xPoint p) {
      if (this.x > p.x) this.x = p.x;
      if (this.y > p.y) this.y = p.y;
   }

   public int dist2(xPoint p) {
      int xdif = this.x - p.x;
      int ydif = this.y - p.y;
      return (xdif*xdif+ydif*ydif);
   }

   public int dist(xPoint p) {
      int xdif = this.x - p.x;
      int ydif = this.y - p.y;
      return (int)(Math.sqrt(xdif*xdif+ydif*ydif));
   }

}



Kelly Waters
Mon Oct 27 18:18:15 EST 1997