home *** CD-ROM | disk | FTP | other *** search
/ ring.yamanashi.ac.jp/pub/pc/freem/action/ / action.zip / a7xpg0_11.zip / a7xpg / src / abagames / util / Vector.d < prev   
Text File  |  2003-09-20  |  2KB  |  94 lines

  1. /*
  2.  * $Id: Vector.d,v 1.1.1.1 2003/09/19 14:55:49 kenta Exp $
  3.  *
  4.  * Copyright 2003 Kenta Cho. All rights reserved.
  5.  */
  6. module abagames.util.Vector;
  7.  
  8. import math;
  9.  
  10. /**
  11.  * Vector.
  12.  */
  13. public class Vector {
  14.  public:
  15.   float x, y;
  16.  
  17.  private:
  18.  
  19.   public this() {}
  20.  
  21.   public this(float x, float y) {
  22.     this.x = x; this.y = y;
  23.   }
  24.  
  25.   public float innerProduct(Vector v) {
  26.     return x * v.x + y * v.y;
  27.   }
  28.  
  29.   public Vector getElement(Vector v) {
  30.     Vector rsl;
  31.     float ll = v.x * v.x + v.y * v.y;
  32.     if (ll != 0) {
  33.       float mag = innerProduct(v);
  34.       rsl.x = mag * v.x / ll;
  35.       rsl.y = mag * v.y / ll;
  36.     } else {
  37.       rsl.x = rsl.y = 0; 
  38.     }
  39.     return rsl;
  40.   }
  41.  
  42.   public void add(Vector v) {
  43.     x += v.x;
  44.     y += v.y;
  45.   }
  46.  
  47.   public void sub(Vector v) {
  48.     x -= v.x;
  49.     y -= v.y;
  50.   }
  51.  
  52.   public void mul(float a) {    
  53.     x *= a;
  54.     y *= a;
  55.   }
  56.  
  57.   public void div(float a) {    
  58.     x /= a;
  59.     y /= a;
  60.   }
  61.  
  62.   public int checkSide(Vector pos1, Vector pos2) {
  63.     int xo = pos2.x - pos1.x;
  64.     int yo = pos2.y - pos1.y;
  65.     if (xo == 0) {
  66.       if (yo == 0)
  67.     return 0;
  68.       return x - pos1.x;
  69.     } else if (yo == 0) {
  70.       return pos1.y - y;
  71.     } else {
  72.       if (xo * yo > 0) { 
  73.     return (x - pos1.x) / xo - (y - pos1.y) / yo;
  74.       } else {
  75.     return -(x - pos1.x) / xo + (y - pos1.y) / yo;
  76.       }
  77.     }
  78.   }
  79.  
  80.   public float size() {
  81.     return sqrt(x * x + y * y);
  82.   }
  83.  
  84.   public float dist(Vector v) {
  85.     float ax = fabs(x - v.x);
  86.     float ay = fabs(y - v.y);
  87.     if ( ax > ay ) {
  88.       return ax + ay / 2;
  89.     } else {
  90.       return ay + ax / 2;
  91.     }
  92.   }
  93. }
  94.