Advertisement
StormWingDelta

My Java 2D Classes Im Working On

Mar 20th, 2012
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.12 KB | None | 0 0
  1. //Keep in mind I'm still working on this and adding things to it.
  2. public class CJRVector2D
  3. {
  4.     private CJRPoint2D xy;
  5.    
  6.     // Default constructor
  7.     public CJRVector2D() { this.Set(0,0); }
  8.    
  9.     // Constructor
  10.     public CJRVector2D( int x, int y ) { this.Set((double)x,(double)y); }
  11.     public CJRVector2D( double x, double y ) { this.Set(x,y); }
  12.     public CJRVector2D( float x, float y ) { this.Set((double)x,(double)y); }
  13.    
  14.     // Accessor Methods
  15.     public double GetX() { return xy.X(); }
  16.     public double GetY() { return xy.Y(); }
  17.    
  18.     // Mutator Methods
  19.     public void SetX(double x) { xy.setX(x); }
  20.     public void SetY(double y) { xy.setY(y); }
  21.     public void Set(double x, double y) { this.SetX(x); this.SetY(y); }
  22.     public void SetX(int x) { xy.setX((double)x); }
  23.     public void SetY(int y) { xy.setY((double)y); }
  24.     public void Set(int x, int y) { this.SetX(x); this.SetY(y); }
  25.     public void SetX(float x) { xy.setX((double)x); }
  26.     public void SetY(float y) { xy.setY((double)y); }
  27.     public void Set(float x, float y) { this.SetX(x); this.SetY(y); }
  28.    
  29.     // Display Methods
  30.     public void Display() { System.out.print("(" + this.GetX() + "," + this.GetY() + ")"); }
  31.     public void DisplayLn() { System.out.println("(" + this.GetX() + "," + this.GetY() + ")"); }
  32.    
  33.     public void Negate() { this.Set((this.GetX() * -1),(this.GetY() * -1)); }
  34.    
  35.     public double LengthSquared() { return (this.GetX() * this.GetX() + this.GetY() * this.GetY()); }
  36.     public double Length() { return Math.sqrt(this.GetX() * this.GetX() + this.GetY() * this.GetY()); }
  37.    
  38.     //ASMD of Vectors With Vectors
  39.     public void Add(CJRVector2D v2)
  40.     {
  41.         double X = this.GetX() + v2.GetX();
  42.         double Y = this.GetY() + v2.GetY();
  43.         this.Set(X,Y);
  44.     }
  45.     public void Subtract(CJRVector2D v2)
  46.     {
  47.         double X = this.GetX() - v2.GetX();
  48.         double Y = this.GetY() - v2.GetY();
  49.         this.Set(X,Y);
  50.     }
  51.     public void Multiply(CJRVector2D v2)
  52.     {
  53.         double X = this.GetX() * v2.GetX();
  54.         double Y = this.GetY() * v2.GetY();
  55.         this.Set(X,Y);
  56.     }
  57.     public void Divide(CJRVector2D v2)
  58.     {
  59.         double X = this.GetX() / v2.GetX();
  60.         double Y = this.GetY() / v2.GetY();
  61.         this.Set(X,Y);
  62.     }
  63.    
  64.     //Scalers Versions
  65.     public void Add( double d )
  66.     {
  67.         double X = this.GetX() + d;
  68.         double Y = this.GetY() + d;
  69.         this.Set(X,Y);
  70.     }
  71.     public void Subtract( double d )
  72.     {
  73.         double X = this.GetX() - d;
  74.         double Y = this.GetY() - d;
  75.         this.Set(X,Y);
  76.     }
  77.     public void Multiply( double d )
  78.     {
  79.         double X = this.GetX() * d;
  80.         double Y = this.GetY() * d;
  81.         this.Set(X,Y);
  82.     }
  83.     public void Divide( double d )
  84.     {
  85.         double X = this.GetX() / d;
  86.         double Y = this.GetY() / d;
  87.         this.Set(X,Y);
  88.     }
  89.     public void Add( int d )
  90.     {
  91.         int X = (int)this.GetX() + d;
  92.         int Y = (int)this.GetY() + d;
  93.         this.Set(X,Y);
  94.     }
  95.     public void Subtract( int d )
  96.     {
  97.         int X = (int)this.GetX() - d;
  98.         int Y = (int)this.GetY() - d;
  99.         this.Set(X,Y);
  100.     }
  101.     public void Multiply( int d )
  102.     {
  103.         int X = (int)this.GetX() * d;
  104.         int Y = (int)this.GetY() * d;
  105.         this.Set(X,Y);
  106.     }
  107.     public void Divide( int d )
  108.     {
  109.         int X = (int)this.GetX() / d;
  110.         int Y = (int)this.GetY() / d;
  111.         this.Set(X,Y);
  112.     }
  113.     public void Add( float d )
  114.     {
  115.         float X = (float)this.GetX() + d;
  116.         float Y = (float)this.GetY() + d;
  117.         this.Set(X,Y);
  118.     }
  119.     public void Subtract( float d )
  120.     {
  121.         float X = (float)this.GetX() - d;
  122.         float Y = (float)this.GetY() - d;
  123.         this.Set(X,Y);
  124.     }
  125.     public void Multiply( float d )
  126.     {
  127.         float X = (float)this.GetX() * d;
  128.         float Y = (float)this.GetY() * d;
  129.         this.Set(X,Y);
  130.     }
  131.     public void Divide( float d )
  132.     {
  133.         float X = (float)this.GetX() / d;
  134.         float Y = (float)this.GetY() / d;
  135.         this.Set(X,Y);
  136.     }
  137.    
  138.     public void Zero() { this.Set(0,0); System.out.println("(" + xy.X() + "," + xy.Y() + ")"); }
  139.    
  140.     public boolean Equals(CJRVector2D v2)
  141.     {
  142.         boolean Xx = (this.GetX() == v2.GetX());
  143.         boolean Yy = (this.GetY() == v2.GetY());
  144.         if(Xx && Yy)
  145.         {
  146.             return true;
  147.         }
  148.         else
  149.         {
  150.             return false;
  151.         }
  152.     }
  153.     public boolean NotEquals(CJRVector2D v2)
  154.     {
  155.         boolean Xx = (this.GetX() == v2.GetX());
  156.         boolean Yy = (this.GetY() == v2.GetY());
  157.         if(!Xx && !Yy)
  158.         {
  159.             return true;
  160.         }
  161.         else
  162.         {
  163.             return false;
  164.         }
  165.     }
  166.    
  167.     public double DotProduct( CJRVector2D v2 )
  168.     {
  169.         double XX = (this.GetX() * v2.GetX());
  170.         double YY = (this.GetY() * v2.GetY());
  171.         return XX + YY;
  172.     }
  173.     public double CrossProduct( CJRVector2D v2 )
  174.     {
  175.         double XX = (this.GetX() * v2.GetX());
  176.         double YY = (this.GetY() * v2.GetY());
  177.         return XX - YY;
  178.     }
  179. }
  180.  
  181. //The point 2d that goes with it
  182. public class CJRPoint2D extends Object
  183. {
  184.     private double x, y;
  185.  
  186.     public CJRPoint2D()
  187.     {
  188.         setX(0);
  189.         setY(0);
  190.     }
  191.    
  192.     //int constructor
  193.     public CJRPoint2D(int x, int y) {
  194.         setX(x);
  195.         setY(y);
  196.     }
  197.     //float constructor
  198.     public CJRPoint2D(float x, float y) {
  199.         setX(x);
  200.         setY(y);
  201.     }
  202.     //double constructor
  203.     public CJRPoint2D(double x, double y) {
  204.         setX(x);
  205.         setY(y);
  206.     }
  207.  
  208.     //X property
  209.     double X() { return x; }
  210.     public void setX(double x) { this.x = x; }
  211.     public void setX(float x) { this.x = (double) x; }
  212.     public void setX(int x) { this.x = (double) x; }
  213.  
  214.     //Y property
  215.     double Y() { return y; }
  216.     public void setY(double y) { this.y = y; }
  217.     public void setY(float y) { this.y = (double) y; }
  218.     public void setY(int y) { this.y = (double) y; }
  219. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement