Back to index

wims  3.65+svn20090927
LineObject.java
Go to the documentation of this file.
00001 package rene.zirkel.objects;
00002 
00003 // file: LineObject.java
00004 
00005 import rene.util.xml.XmlWriter;
00006 import rene.zirkel.*;
00007 import rene.zirkel.construction.*;
00008 import rene.zirkel.graphics.*;
00009 
00010 public class LineObject extends TwoPointLineObject
00011 {      static Count N=new Count();
00012 
00013        public LineObject (Construction c, PointObject p1, PointObject p2)
00014        {      super(c,p1,p2);
00015               validate();
00016               updateText();
00017        }
00018 
00019        public String getTag () { return "Line"; }
00020 
00021        public void updateText()
00022        {      setText(text2(Zirkel.name("text.line"),P1.getName(),P2.getName()));
00023        }
00024        public void validate ()
00025        {      if (!P1.valid() || !P2.valid()) { Valid=false; return; }
00026               else
00027               {      Valid=true;
00028                      X1=P1.getX(); Y1=P1.getY();
00029                      X2=P2.getX(); Y2=P2.getY();
00030                      // compute normalized vector in the direction of the line:
00031                      DX=X2-X1; DY=Y2-Y1; 
00032                      R=Math.sqrt(DX*DX+DY*DY);
00033                      if (R<1e-10) { Valid=false; return; }
00034                      DX/=R; DY/=R;
00035               }
00036        }
00037        
00038        public void paint (MyGraphics g, ZirkelCanvas zc)
00039        {      if (!Valid || mustHide(zc)) return;
00040               if (!Partial || zc.showHidden())
00041               {      super.paint(g,zc);
00042                      return;
00043               }
00044               //compute middle of the screen:
00045               double xm=(zc.minX()+zc.maxX())/2,ym=(zc.minY()+zc.maxY())/2;
00046               // compute distance from middle to line:
00047               double d=(xm-X1)*DY-(ym-Y1)*DX;
00048               // compute point with minimal distance
00049               double x=xm-d*DY,y=ym+d*DX;
00050               // compute size of the screen
00051               double a=Math.max(zc.maxX()-zc.minX(),zc.maxY()-zc.minY());
00052               if (Math.abs(d)>a) return;
00053               // compute distance from closest point to source
00054               double b=(x-X1)*DX+(y-Y1)*DY;
00055               // compute the two visible endpoints
00056               k1=b-a; k2=b+a; k12valid=true;
00057               double dd=(zc.maxX()-zc.minX())/20;
00058               double dmin=-dd,dmax=R+dd;
00059               if (Dep!=null)
00060               {      for (int i=0; i<NDep; i++)
00061                      {      if (!Dep[i].valid() || Dep[i].mustHide(zc)) continue;
00062                             double s=project(Dep[i].getX(),Dep[i].getY());
00063                             if (s-dd<dmin) dmin=s-dd;
00064                             else if (s+dd>dmax) dmax=s+dd;
00065                      }
00066               }
00067               if (k1<dmin) k1=dmin;
00068               if (k2>dmax) k2=dmax;
00069               double c1=zc.col(X1+k1*DX),c2=zc.col(X1+k2*DX),
00070                      r1=zc.row(Y1+k1*DY),r2=zc.row(Y1+k2*DY);
00071               // paint:
00072               if (isStrongSelected() && g instanceof MyGraphics13)
00073               {      ((MyGraphics13)g).drawMarkerLine(c1,r1,c2,r2);
00074               }
00075               g.setColor(this);
00076               g.drawLine(c1,r1,c2,r2,this);
00077               String s=getDisplayText();
00078               if (!s.equals(""))
00079               {      g.setLabelColor(this);
00080                      setFont(g);
00081                      DisplaysText=true;
00082                      if (KeepClose)
00083                      {      double side=(YcOffset<0)?1:-1;
00084                             drawLabel(g,s,zc,X1+XcOffset*DX,Y1+XcOffset*DY,
00085                                    side*DX,side*DY,0,0);
00086                      }
00087                      else
00088                             drawLabel(g,s,zc,X1+k2*DX/2,Y1+k2*DY/2,
00089                                    DX,DY,XcOffset,YcOffset);
00090               }             
00091        }
00092 
00093        public void printArgs (XmlWriter xml)
00094        {      xml.printArg("from",P1.getName());
00095               xml.printArg("to",P2.getName());
00096               super.printArgs(xml);
00097        }
00098 
00099        public void setDefaults ()
00100        {      super.setDefaults();
00101               setPartial(Cn.PartialLines);
00102        }
00103 
00104        public void toggleHidden ()
00105        {      if (Hidden)
00106               {      Partial=false;
00107                      Hidden=false;
00108               }
00109               else
00110               {      if (Partial)
00111                      {      Partial=false;
00112                             Hidden=true;
00113                      }
00114                      else Partial=true;
00115               }
00116        }
00117        
00118        public boolean hasUnit ()
00119        {      return false;
00120        }
00121 
00122        public boolean nearto (int c, int r, ZirkelCanvas zc)
00123        {      if (!displays(zc)) return false;
00124               if (zc.showHidden() || !Partial || Dep==null || !k12valid) 
00125                      return super.nearto(c,r,zc);
00126               //compute point at c,r
00127               double x=zc.x(c),y=zc.y(r);
00128               // test, if on visible part
00129               double s=project(x,y);
00130               if (s<k1 || s>k2) return false;
00131               // compute distance from x,y
00132               double d=(x-X1)*DY-(y-Y1)*DX;
00133               // scale in screen coordinates
00134               Value=Math.abs(zc.col(zc.minX()+d)-zc.col(zc.minX()));
00135               return Value<zc.selectionSize()*2;
00136        }
00137 }