]> Shamusworld >> Repos - architektonas/blobdiff - src/line.cpp
Added 'Fixed Angle' functionality to Line.
[architektonas] / src / line.cpp
index 385e086e9cdfc8036928e2116b481aac723e72ae..d6169d64a0a593649171c83348a1a2a6341b6047 100644 (file)
@@ -13,7 +13,7 @@
 //                  "Fixed Length" button is down
 // JLH  04/27/2011  Fixed attached dimension to stay a correct length when
 //                  "Fixed Length" button is *not* down ;-)
-// JLH  05/29/2011  Added mouseover hints
+// JLH  05/29/2011  Added (some) mouseover hints
 //
 
 #include "line.h"
 
 Line::Line(Vector p1, Vector p2, Object * p/*= NULL*/): Object(p1, p), endpoint(p2),
        draggingLine(false), draggingHandle1(false), draggingHandle2(false), //needUpdate(false),
-       length(Vector::Magnitude(p2, p1)), hitPoint1(false), hitPoint2(false), hitLine(false)
+       length(Vector::Magnitude(p2, p1)), angle(Vector(endpoint - position).Unit()),
+       hitPoint1(false), hitPoint2(false), hitLine(false)
 {
 }
 
 Line::~Line()
 {
+       // If there are any attached Dimensions, we must set the attachment points
+       // to NULL since they will no longer be valid.
+       if (attachedDimension)
+       {
+               attachedDimension->SetPoint1(NULL);
+               attachedDimension->SetPoint2(NULL);
+       }
+       // IT WOULD BE NICE to have any object points attached to this line automagically
+       // connect to this dimension object at this point, instead of just becoming
+       // detached.
 }
 
 /*virtual*/ void Line::Draw(QPainter * painter)
@@ -83,6 +94,17 @@ There's a small problem here with the implementation: You can have a dimension t
 to only one point while at the same time you can have a dimension sitting on this line.
 Since there's only *one* dimPoint for each point, this can be problematic...
 
+We solve this by allowing only *one* Dimension object to be attached to the Line,
+Arc, etc. and by giving the Dimension object a pointer to our endpoints.
+
+Problem still arises when we delete this object; The attached Dimension object will
+then have bad pointers! What it *should* do is delete the object if and only if this
+line is not attached to any other object. If it is, then one of those attachment
+points should be sent to the dimension object (done for position & endpoint).
+
+NOTE: The STL vector<T> *does not* take ownership of pointers, therefore is suitable
+      for our purposes
+
 Also: It would be nice to have a preview of the dimension being drawn, with a modifier
 key to make it draw/show on the other side...
 
@@ -236,6 +258,7 @@ software currently out there: the GUI will try to do the right thing, most of th
                Vector point1 = (draggingHandle1 ? endpoint : position);
                Vector point2 = (draggingHandle1 ? position : endpoint);
 
+#if 0
                Vector current(point2, point1);
                Vector v = current.Unit() * length;
                Vector v2 = point1 + v;
@@ -243,6 +266,22 @@ software currently out there: the GUI will try to do the right thing, most of th
                //bleh
                if (!Object::fixedLength)
                        v2 = point2;
+#endif
+
+               if (Object::fixedAngle)
+               {
+                       // Here we calculate the component of the current vector along the fixed angle.
+                       // A_compB = (A . Bu) * Bu
+                       double magnitudeAlongB = Vector::Dot(Vector(point2 - point1), angle);
+
+                       if (draggingHandle1)
+                               position = endpoint + (angle * magnitudeAlongB);
+
+                       if (draggingHandle2)
+                               endpoint = position + (angle * magnitudeAlongB);
+               }
+//             else
+//                     v2 = point2;
 
 //If we tell the dimension to flip sides, this is no longer a valid
 //assumption. !!! FIX !!!
@@ -286,6 +325,13 @@ software currently out there: the GUI will try to do the right thing, most of th
                        // it turns out to have a fixed length. :-)
                        length = Vector(endpoint - position).Magnitude();
                }
+
+               if (!Object::fixedAngle)
+               {
+                       // Calculate the new angle, just in case on the next move it turns out to
+                       // be fixed. :-)
+                       angle = Vector(endpoint - position).Unit();
+               }
        }
 
        draggingLine = false;