Back to index

wims  3.65+svn20090927
PointonObjectIntersectionObject.java
Go to the documentation of this file.
00001 /*
00002  * Created on 28.10.2005
00003  *
00004  */
00005 package rene.zirkel.objects;
00006 
00007 import rene.util.xml.XmlWriter;
00008 import rene.zirkel.construction.Construction;
00009 
00019 public class PointonObjectIntersectionObject extends IntersectionObject
00020 {      public double Eps=1e-5;
00021        
00022        public PointonObjectIntersectionObject (Construction c,
00023               ConstructionObject p1, ConstructionObject p2)
00024        {      super(c,p1,p2);
00025        }
00026        
00027        public void validate (double x, double y)
00028        {      setXY(x,y);
00029               validate();
00030        }
00031 
00032        public void printArgs (XmlWriter xml)
00033        {      xml.printArg("first",P1.getName());
00034               xml.printArg("second",P2.getName());
00035               xml.printArg("x",""+getX());
00036               xml.printArg("y",""+getY());
00037               if (getAway()!=null)
00038               {      if (StayAway) xml.printArg("awayfrom",getAway().getName());
00039                      else xml.printArg("closeto",getAway().getName());
00040               }
00041               printType(xml);
00042               if (!Restricted) xml.printArg("valid","true");
00043        }
00044 
00053        public void validate ()
00054        {      if (!P1.valid() || !P2.valid()) Valid=false;
00055               else Valid=true;
00056               if (!Valid) return;
00057               double distold=projectOnce();
00058               if (!Valid) return;
00059               for (int i=0; i<10; i++)
00060               {      double dist1=projectOnce();
00061                      if (dist1>=distold && dist1<Eps) return;
00062                      distold=dist1;
00063                      double a=(x1-x),b=(y1-y);
00064                      double s=Math.max(Math.abs(a),Math.abs(b)); 
00065                      if (Math.abs(s)>1e-13) a/=s; b/=s;
00066                      double c=(X-x1),d=(Y-y1);
00067                      s=Math.max(Math.abs(c),Math.abs(d)); 
00068                      if (Math.abs(s)>1e-13) c/=s; d/=s;
00069                      double e=a*x1+b*y1,f=c*X+d*Y;
00070                      double det=a*d-c*b;
00071                      if (Math.abs(det)>1e-13)
00072                      {      double xn=(e*d-f*b)/det;
00073                             double yn=(a*f-c*e)/det;
00074                             double xold=X,yold=Y;
00075                             X=xn; Y=yn;
00076                             double dist2=projectOnce();
00077                             if (dist2>dist1) // interpolation does not work
00078                             {      X=xold; Y=yold;
00079                             }
00080                             else distold=dist2;
00081                      }
00082               }
00083               Valid=false;
00084        }
00085        
00086        double x,y,x1,y1;
00087        
00088        public double projectOnce ()
00089        {      x=X; y=Y;
00090               // System.out.println("Before: "+X+" "+Y);
00091               ((PointonObject)P1).project(this);
00092               // System.out.println("Projected to "+P1.getName()+": "+X+" "+Y);
00093               double dist=Math.max(Math.abs(X-x),Math.abs(Y-y));
00094               x1=X; y1=Y;
00095               ((PointonObject)P2).project(this);
00096               // System.out.println("Projected to "+P2.getName()+": "+X+" "+Y);
00097               double dist1=Math.max(Math.abs(X-x1),Math.abs(Y-y1));
00098               return Math.max(dist,dist1);
00099        }
00100        
00101        public boolean moveable ()
00102        {      return true;
00103        }
00104        
00105 }
00106