[wadalabfont-kit] / java / Point.java  

View of /java/Point.java

Parent Directory | Revision Log
Revision: 1.2 - (download) (as text) (annotate)
Wed Jan 3 02:46:31 2001 UTC (23 years, 11 months ago) by ktanaka
Branch: MAIN
Changes since 1.1: +1 -1 lines
 change little in shogatsu
//
// 
//
// $B:BI87O$,2hLL:BI87O(B($B:8>e$,(B(0,0))$B$G$"$k$3$H$KCm0U$9$k(B
//
public final class Point {
  double x,y;
  // 10^-5$B0J2<$O(B0$B$H8+$J$9!%(B
  static final double EPS=10e-5;
  Point(double x,double y){ this.x=x; this.y=y; }
  public double getX() { return x;}
  public double getY() { return y;}
  public String toString(){
    return "Point("+x+","+y+")";
  }
  public double length(){
    return Math.sqrt(x*x+y*y);
  }
  public double distance(Point p){
    double dx=x-p.getX(),dy=y-p.getY();
    return Math.sqrt(dx*dx+dy*dy);
  }
  public Point plus(Point p){
    return new Point(x+p.getX(),y+p.getY());
  }
  public Point minus(Point p){
    return new Point(x-p.getX(),y-p.getY());
  }
  public Point times(double r){
    return new Point(x*r,y*r);
  }
  // $B%Y%/%?!<$ND9$5$r(B newLength$B$K@55,2=$9$k!%(B
  public Point normal(double newLength){
    double ratio=newLength/length();
    return new Point(x*ratio,y*ratio);
  }
  // 2$B$D$NE@$NJ,3dE@$rJV$9!%(B
  // ratio$B$,(B0$B$N;~$O85$NE@!%(B1$B$N;~$OAj<j$NE@(B
  public Point inter(Point p,double ratio){
    return times(1-ratio).plus(p.times(ratio));
  }
  // $B%Y%/%H%k$N(B90$BEY2sE>(B
  public Point rot90(){
    return new Point(y,-x);
  }
  // $B%Y%/%H%k$N(B270$BEY2sE>(B
  public Point rot270(){
    return new Point(-y,x);
  }
  // $B<+J,$H%Y%/%?!<(Bp$B$NFb@Q(B(inner product)
  public double product(Point p){
    return x*p.x+y*p.y;
  }
  // $B<+J,$+$i%Y%/%?!<(Bp$B$r8+$?3QEY$N(B cos
  public double cosTheta(Point p){
    return product(p)/(p.length()*length());
  }
  // $B<+J,$+$i%Y%/%?!<(Bp$B$r8+$?3QEY$N(B sin
  public double sinTheta(Point p){
    return rot270().cosTheta(p);
  }
  // $B<+J,$+$i%Y%/%?!<(B p $B$r8+$?3QEY(B
  public double theta(Point p){
    return Math.atan2(sinTheta(p),cosTheta(p));
  }
  // $BD>@~(B p0-p1, p2-p3 $B$N8rE@(B
  // x = p0.x+t*(p1.x-p0.x) = p2.x+s*(p3.x-p2.x)
  // y = p0.y+t*(p1.y-p0.y) = p2.y+s*(p3.y-p2.y)
  public static Point cross(Point p0,Point p1,Point p2,Point p3){
    double dy10=p1.y-p0.y,dx10=p1.x-p0.x;
    double d=dy10*(p3.x-p2.x)-dx10*(p3.y-p2.y);
    // $B9TNs<0$,>.$5$$$H$-$O(B null $B$rJV$9!%(B
    if(Math.abs(d)<EPS) return null;
    double s=(dy10*(p0.x-p2.x)-dx10*(p0.y-p2.y))/d;
    return p2.inter(p3,s);
  }
}


ktanaka

Powered by ViewCVS 1.0-dev

ViewCVS and CVS Help