[wadalabfont-kit] / java / Point.java  

View of /java/Point.java

Parent Directory | Revision Log
Revision: 1.3 - (download) (as text) (annotate)
Thu Jan 4 10:23:56 2001 UTC (23 years, 4 months ago) by ktanaka
Branch: MAIN
CVS Tags: SNAP-20030702, SNAP-20030624, SNAP-20030630, SNAP-20040518, HEAD
Changes since 1.2: +21 -2 lines
MinchoRenderer$B$N$X$s$3$&(B
//
// $B:BI87O$,2hLL:BI87O(B($B:8>e$,(B(0,0))$B$G$"$k$3$H$KCm0U$9$k(B
//
//        (0,-1)
// (-1,0) (0,0)  (1,0)
//        (0,1)
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 neg(){
    return new Point(-x,-y);
  }
  public Point times(double r){
    return new Point(x*r,y*r);
  }
  public Point timesX(double r){
    return new Point(x*r,y);
  }
  public Point timesY(double r){
    return new Point(x,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);
  }
  // $BG$0UEY$N2sE>(B
  public Point rot(double theta){
    double cosTheta=Math.cos(theta),sinTheta=Math.sin(theta);
    return new Point(x*cosTheta+y*sinTheta,-x*sinTheta+y*cosTheta);
  }
  // $B<+J,$H%Y%/%?!<(Bp$B$NFb@Q(B(inner product)
  public double product(Point p){
    return x*p.x+y*p.y;
  }
  // (1,0)$B$+$i8+$?<+J,$N3QEY(B
  public double theta(){
    return Math.atan2(-y,x);
  }
  // $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