]> git.gag.com Git - sw/motorsim/commitdiff
Pulled math into a subobject.
authorBill Kuker <bkuker@billkuker.com>
Wed, 15 Apr 2009 14:31:50 +0000 (14:31 +0000)
committerBill Kuker <bkuker@billkuker.com>
Wed, 15 Apr 2009 14:31:50 +0000 (14:31 +0000)
Still need to figure out the littel artifacts I get because the tan function isn't perfect.

src/TriTest.java

index 03875b7b8cb98be7654290cfec6f5d80b23a0523..30b1aaab8f36086304234df455268ae2efd04f31 100644 (file)
@@ -19,94 +19,100 @@ import javax.swing.event.ChangeListener;
 \r
 public class TriTest extends JPanel {\r
        \r
+       private class RegressableShape {\r
+               private Area a;\r
+               public RegressableShape( Shape s ){\r
+                       if ( s instanceof Area )\r
+                               a = (Area)s;\r
+                       a = new Area(s);\r
+               }\r
+               \r
+               public Area getRegressedShape(double regression){\r
+                       Area ret = new Area();  \r
+                       \r
+                       PathIterator i = a.getPathIterator(new AffineTransform(), .001);\r
+                       double last[] = {0,0};\r
+                       double first[] = {0,0};\r
+                       while (!i.isDone()) {\r
+                               double coords[] = new double[6];\r
+                               int type = i.currentSegment(coords);\r
+                               switch (type){\r
+                                       case PathIterator.SEG_MOVETO:\r
+                                               first[0] = last[0] = coords[0];\r
+                                               first[1] = last[1] = coords[1];\r
+                                               break;\r
+                                       case PathIterator.SEG_CLOSE:\r
+                                               coords[0] = first[0];\r
+                                               coords[1] = first[1];\r
+                                       case PathIterator.SEG_LINETO:\r
+                                               //Do it;\r
+                                               double len = Math.sqrt(Math.pow(last[0] - coords[0], 2) + Math.pow(last[1] - coords[1], 2));\r
+                                               double dx = coords[0]-last[0];\r
+                                               double dy = coords[1]-last[1];\r
+                                               double angle = Math.atan2(dy, dx);\r
+                                                               \r
+                                               Area rect;\r
+                                               if ( regression > 0 )\r
+                                                       rect = new Area(new Rectangle2D.Double(0,0,len,regression));\r
+                                               else\r
+                                                       rect = new Area(new Rectangle2D.Double(0,regression,len,-regression));\r
+                                               rect.transform(AffineTransform.getRotateInstance(angle));\r
+                                               rect.transform(AffineTransform.getTranslateInstance(last[0], last[1]));\r
+                                               ret.add(rect);\r
+                                               \r
+                                               if ( regression > 0 ){\r
+                                                       ret.add(new Area(new Ellipse2D.Double(coords[0]-regression, coords[1]-regression, regression*2, regression*2)));\r
+                                               } else {\r
+                                                       ret.add(new Area(new Ellipse2D.Double(coords[0]+regression, coords[1]+regression, -regression*2, -regression*2)));\r
+                                               }\r
+                                               \r
+                                               \r
+                                               last[0] = coords[0];\r
+                                               last[1] = coords[1];\r
+                                               break;\r
+                                       case PathIterator.SEG_CUBICTO:\r
+                                       case PathIterator.SEG_QUADTO:\r
+                                               throw new Error("Unflattend Geometry!");\r
+                               }\r
+                               i.next();\r
+                       }\r
+                       \r
+                       if ( regression > 0 ){\r
+                               ret.add(a);\r
+                       }else{\r
+                               Area acp = (Area)a.clone();\r
+                               acp.subtract(ret);\r
+                               ret = acp;\r
+                       }\r
+                       return ret;\r
+               }\r
+       }\r
+       \r
        double r = 0;\r
+       \r
+       RegressableShape shape;\r
+       {\r
+               GeneralPath p = new GeneralPath();\r
+               p.moveTo(100,100);\r
+               p.lineTo(200, 200);\r
+               p.lineTo(100, 300);\r
+               p.closePath();\r
+               shape = new RegressableShape(p);\r
+       }\r
+       \r
        @Override\r
        public void paint(Graphics gg){\r
                Graphics2D g = (Graphics2D)gg;\r
                \r
                g.clearRect(0, 0, 600, 600);\r
-               GeneralPath p;\r
-               \r
-               p = new GeneralPath();\r
-               p.moveTo(100,100);\r
-               p.lineTo(200, 150);\r
-               p.lineTo(150, 180);\r
-               p.lineTo(100, 300);\r
-               p.closePath();\r
-               g.setColor(Color.red);\r
-               g.draw(growArea(p, r));\r
-               g.setColor(Color.black);                \r
-               g.draw(p);\r
-               \r
-               \r
-               p = new GeneralPath();\r
-               p.moveTo(10,10);\r
-               p.lineTo(50, 20);\r
-               p.lineTo(60, 30);\r
-               p.closePath();\r
+\r
                g.setColor(Color.red);\r
-               g.draw(growArea(p, 10));\r
-               g.setColor(Color.black);                \r
-               g.draw(p);\r
+               g.draw(shape.getRegressedShape(r));\r
+               //g.setColor(Color.black);              \r
+               //g.draw(shape.getRegressedShape(0));\r
                \r
        }\r
        \r
-       private Area growArea( Shape a, double sz ){\r
-               Area ret = new Area();\r
-               \r
-               PathIterator i = a.getPathIterator(new AffineTransform(), .001);\r
-               double last[] = {0,0};\r
-               double first[] = {0,0};\r
-               while (!i.isDone()) {\r
-                       double coords[] = new double[6];\r
-                       int type = i.currentSegment(coords);\r
-                       switch (type){\r
-                               case PathIterator.SEG_MOVETO:\r
-                                       first[0] = last[0] = coords[0];\r
-                                       first[1] = last[1] = coords[1];\r
-                                       break;\r
-                               case PathIterator.SEG_CLOSE:\r
-                                       coords[0] = first[0];\r
-                                       coords[1] = first[1];\r
-                               case PathIterator.SEG_LINETO:\r
-                                       //Do it;\r
-                                       double len = Math.sqrt(Math.pow(last[0] - coords[0], 2) + Math.pow(last[1] - coords[1], 2));\r
-                                       double dx = coords[0]-last[0];\r
-                                       double dy = coords[1]-last[1];\r
-                                       double angle = Math.atan2(dy, dx);\r
-                                                       \r
-                                       Area rect;\r
-                                       if ( sz > 0 )\r
-                                               rect = new Area(new Rectangle2D.Double(0,-sz,len,sz));\r
-                                       else\r
-                                               rect = new Area(new Rectangle2D.Double(0,0,len,-sz));\r
-                                       rect.transform(AffineTransform.getRotateInstance(angle));\r
-                                       rect.transform(AffineTransform.getTranslateInstance(last[0], last[1]));\r
-                                       ret.add(rect);\r
-                                       \r
-                                       if ( sz > 0 ){\r
-                                               ret.add(new Area(new Ellipse2D.Double(coords[0]-sz, coords[1]-sz, sz*2, sz*2)));\r
-                                       } else {\r
-                                               ret.add(new Area(new Ellipse2D.Double(coords[0]+sz, coords[1]+sz, -sz*2, -sz*2)));\r
-                                       }\r
-                                       \r
-                                       \r
-                                       last[0] = coords[0];\r
-                                       last[1] = coords[1];\r
-                                       break;\r
-                               case PathIterator.SEG_CUBICTO:\r
-                               case PathIterator.SEG_QUADTO:\r
-                                       throw new Error("Unflattend Geometry!");\r
-                       }\r
-                       i.next();\r
-               }\r
-               \r
-               if ( sz > 0 )\r
-                       ret.add(new Area(a));\r
-               else\r
-                       ret.intersect(new Area(a));\r
-               return ret;\r
-       }\r
        \r
        public static void main( String args[] ){\r
                JFrame f = new JFrame();\r