]> Shamusworld >> Repos - architektonas/blobdiff - src/drawingview.cpp
Added line-to-circle intersection code.
[architektonas] / src / drawingview.cpp
index 5ae9c9a9081e5ccf121c82958eac22e6634d6400..0e563e7e892e3ecba6e563d8af1b7798ee8e05ba 100644 (file)
@@ -7,7 +7,7 @@
 // JLH = James Hammons <jlhamm@acm.org>
 //
 // Who  When        What
-// ---  ----------  -------------------------------------------------------------
+// ---  ----------  ------------------------------------------------------------
 // JLH  03/22/2011  Created this file
 // JLH  09/29/2011  Added middle mouse button panning
 //
@@ -91,8 +91,8 @@ DrawingView::DrawingView(QWidget * parent/*= NULL*/): QWidget(parent),
        document.objects.push_back(new Line(Vector(10, 83), Vector(17, 2)));
        document.objects.push_back(new Circle(Vector(100, 100), 36));
        document.objects.push_back(new Circle(Vector(50, 150), 49));
-       document.objects.push_back(new Arc(Vector(300, 300), 32, PI / 4.0, PI * 1.3)),
-       document.objects.push_back(new Arc(Vector(200, 200), 60, PI / 2.0, PI * 1.5));
+       document.objects.push_back(new Arc(Vector(300, 300), 32, TAU / 8.0, TAU * 0.65)),
+       document.objects.push_back(new Arc(Vector(200, 200), 60, TAU / 4.0, TAU * 0.75));
        document.objects.push_back(new Dimension(Vector(50, 40), Vector(5, 5)));
        document.objects.push_back(new Text(Vector(10, 83), "Here is some awesome text!"));
 #endif
@@ -147,20 +147,6 @@ we do! :-)
 }
 
 
-#if 0
-void DrawingView::SetToolActive(Action * action)
-{
-       if (action != NULL)
-       {
-               toolAction = action;
-               connect(toolAction, SIGNAL(ObjectReady(Object *)), this,
-                       SLOT(AddNewObjectToDocument(Object *)));
-               connect(toolAction, SIGNAL(NeedRefresh()), this, SLOT(HandleActionUpdate()));
-       }
-}
-#endif
-
-
 void DrawingView::SetGridSize(uint32_t size)
 {
        // Sanity check
@@ -182,7 +168,6 @@ void DrawingView::SetGridSize(uint32_t size)
        pmp.end();
 
        // Set up new BG brush & zoom level (pixels per base unit)
-//     Painter::zoom = gridPixels / gridSpacing;
        Global::zoom = gridPixels / Global::gridSpacing;
        UpdateGridBackground();
 }
@@ -267,14 +252,6 @@ zero; so we do another modulus operation on the result to achieve this.
 }
 
 
-void DrawingView::SetCurrentLayer(int /*layer*/)
-{
-//Not needed anymore...
-//     Global::currentLayer = layer;
-//printf("DrawingView::CurrentLayer = %i\n", layer);
-}
-
-
 //
 // Basically, we just make a single pass through the Container. If the layer #
 // is less than the layer # being deleted, then do nothing. If the layer # is
@@ -340,9 +317,9 @@ void DrawingView::HandleLayerSwap(int layer1, int layer2)
 
 QPoint DrawingView::GetAdjustedMousePosition(QMouseEvent * event)
 {
-       // This is undoing the transform, e.g. going from client coords to local coords.
-       // In essence, the height - y is height + (y * -1), the (y * -1) term doing the
-       // conversion of the y-axis from increasing bottom to top.
+       // This is undoing the transform, e.g. going from client coords to local
+       // coords. In essence, the height - y is height + (y * -1), the (y * -1)
+       // term doing the conversion of the y-axis from increasing bottom to top.
        return QPoint(offsetX + event->x(), offsetY + (size().height() - event->y()));
 }
 
@@ -351,7 +328,7 @@ QPoint DrawingView::GetAdjustedClientPosition(int x, int y)
 {
        // VOODOO ALERT (ON Y COMPONENT!!!!) (eh?)
        // No voodoo here, it's just grouped wrong to see it. It should be:
-       // -offsetY + (size.height() + (y * -1.0)) <-- this is wrong, offsetY should be positive
+       // -offsetY + (size.height() + (y * -1.0)) <-- this is wrong, offsetY should be positive [why? we use -offsetX after all]
        return QPoint(-offsetX + x, (size().height() - (-offsetY + y)) * +1.0);
 }
 
@@ -477,19 +454,19 @@ void DrawingView::RenderObjects(Painter * painter, std::vector<void *> & v, int
 
                        if (d->subtype == DTLinearVert)
                        {
-                               if ((angle < 0) || (angle > PI))
+                               if ((angle < 0) || (angle > HALF_TAU))
                                {
                                        x1 = (d->p[0].x > d->p[1].x ? d->p[0].x : d->p[1].x);
                                        y1 = (d->p[0].y > d->p[1].y ? d->p[0].y : d->p[1].y);
                                        ortho = Vector(1.0, 0);
-                                       angle = PI3_OVER_2;
+                                       angle = THREE_QTR_TAU;
                                }
                                else
                                {
                                        x1 = (d->p[0].x > d->p[1].x ? d->p[1].x : d->p[0].x);
                                        y1 = (d->p[0].y > d->p[1].y ? d->p[1].y : d->p[0].y);
                                        ortho = Vector(-1.0, 0);
-                                       angle = PI_OVER_2;
+                                       angle = QTR_TAU;
                                }
 
                                linePt1.x = linePt2.x = x1;
@@ -497,7 +474,7 @@ void DrawingView::RenderObjects(Painter * painter, std::vector<void *> & v, int
                        }
                        else if (d->subtype == DTLinearHorz)
                        {
-                               if ((angle < PI_OVER_2) || (angle > PI3_OVER_2))
+                               if ((angle < QTR_TAU) || (angle > THREE_QTR_TAU))
                                {
                                        x1 = (d->p[0].x > d->p[1].x ? d->p[0].x : d->p[1].x);
                                        y1 = (d->p[0].y > d->p[1].y ? d->p[0].y : d->p[1].y);
@@ -509,7 +486,7 @@ void DrawingView::RenderObjects(Painter * painter, std::vector<void *> & v, int
                                        x1 = (d->p[0].x > d->p[1].x ? d->p[1].x : d->p[0].x);
                                        y1 = (d->p[0].y > d->p[1].y ? d->p[1].y : d->p[0].y);
                                        ortho = Vector(0, -1.0);
-                                       angle = PI;
+                                       angle = HALF_TAU;
                                }
 
                                linePt1.y = linePt2.y = y1;
@@ -539,10 +516,9 @@ void DrawingView::RenderObjects(Painter * painter, std::vector<void *> & v, int
                        painter->DrawLine(p3, p5);
                        painter->DrawLine(p4, p6);
 
-                       // Calculate whether or not the arrowheads are too crowded to put inside
-                       // the extension lines. 9.0 is the length of the arrowhead.
+                       // Calculate whether or not the arrowheads are too crowded to put
+                       // inside the extension lines. 9.0 is the length of the arrowhead.
                        double t = Geometry::ParameterOfLineAndPoint(linePt1, linePt2, linePt2 - (unit * 9.0 * scaledThickness));
-               //printf("Dimension::Draw(): t = %lf\n", t);
 
                        // On the screen, it's acting like this is actually 58%...
                        // This is correct, we want it to happen at > 50%
@@ -568,9 +544,6 @@ void DrawingView::RenderObjects(Painter * painter, std::vector<void *> & v, int
                        painter->SetFont(QFont("Arial", 8.0 * Global::zoom * scaledThickness));
                        Point ctr = p2 + (Vector(p2, p1) / 2.0);
 
-               #if 0
-                       QString dimText = QString("%1\"").arg(Vector(endpoint - position).Magnitude());
-               #else
                        QString dimText;
 
                        if (length < 12.0)
@@ -585,7 +558,6 @@ void DrawingView::RenderObjects(Painter * painter, std::vector<void *> & v, int
                                else
                                        dimText = QString("%1' %2\"").arg(feet).arg(inches);
                        }
-               #endif
 
                        painter->DrawAngledText(ctr, angle, dimText, scaledThickness);
 
@@ -744,7 +716,7 @@ void DrawingView::ToolDraw(Painter * painter)
                        double span = angle - toolPoint[2].x;
 
                        if (span < 0)
-                               span += PI_TIMES_2;
+                               span += TAU;
 
                        painter->DrawLine(toolPoint[0], toolPoint[3]);
                        painter->SetBrush(QBrush(Qt::NoBrush));
@@ -974,7 +946,7 @@ void DrawingView::ArcHandler(int mode, Point p)
                        double span = endAngle - toolPoint[2].x;
 
                        if (span < 0)
-                               span += PI_TIMES_2;
+                               span += TAU;
 
                        Arc * arc = new Arc(toolPoint[0], toolPoint[1].x, toolPoint[2].x, span);
                        arc->layer = Global::activeLayer;
@@ -1029,8 +1001,8 @@ void DrawingView::RotateHandler(int mode, Point p)
                                {
                                        obj2->angle[0] = obj.angle[0] + angle;
 
-                                       if (obj2->angle[0] > PI_TIMES_2)
-                                               obj2->angle[0] -= PI_TIMES_2;
+                                       if (obj2->angle[0] > TAU)
+                                               obj2->angle[0] -= TAU;
                                }
                        }
                }
@@ -1132,8 +1104,8 @@ void DrawingView::MirrorHandler(int mode, Point p)
                                        // This is 2*mirror angle - obj angle - obj span
                                        obj2->angle[0] = (2.0 * angle) - obj.angle[0] - obj.angle[1];
 
-                                       if (obj2->angle[0] > PI_TIMES_2)
-                                               obj2->angle[0] -= PI_TIMES_2;
+                                       if (obj2->angle[0] > TAU)
+                                               obj2->angle[0] -= TAU;
                                }
                        }
                }
@@ -1325,9 +1297,17 @@ void DrawingView::mouseMoveEvent(QMouseEvent * event)
                if (numIntersecting > 0)
                {
                        Vector v1 = Global::intersectPoint[0];
+
+                       if (numIntersecting == 2)
+                       {
+                               Vector v2 = Global::intersectPoint[1];
+
+                               if (Vector::Magnitude(v2, point) < Vector::Magnitude(v1, point))
+                                       v1 = v2;
+                       }
+
                        QString text = tr("Intersection <%1, %2>");
                        informativeText = text.arg(v1.x).arg(v1.y);
-
                        hoveringIntersection = true;
                        intersectionPoint = v1;
                }
@@ -1550,28 +1530,28 @@ void DrawingView::CheckObjectBounds(void)
 
                        // If the end of the arc is before the beginning, add 360 degrees to it
                        if (end < start)
-                               end += 2.0 * PI;
+                               end += TAU;
 
                        // Adjust the bounds depending on which axes are crossed
-                       if ((start < PI_OVER_2) && (end > PI_OVER_2))
+                       if ((start < QTR_TAU) && (end > QTR_TAU))
                                bounds.setTop(1.0);
 
-                       if ((start < PI) && (end > PI))
+                       if ((start < HALF_TAU) && (end > HALF_TAU))
                                bounds.setLeft(-1.0);
 
-                       if ((start < (PI + PI_OVER_2)) && (end > (PI + PI_OVER_2)))
+                       if ((start < THREE_QTR_TAU) && (end > THREE_QTR_TAU))
                                bounds.setBottom(-1.0);
 
-                       if ((start < (2.0 * PI)) && (end > (2.0 * PI)))
+                       if ((start < TAU) && (end > TAU))
                                bounds.setRight(1.0);
 
-                       if ((start < ((2.0 * PI) + PI_OVER_2)) && (end > ((2.0 * PI) + PI_OVER_2)))
+                       if ((start < (TAU + QTR_TAU)) && (end > (TAU + QTR_TAU)))
                                bounds.setTop(1.0);
 
-                       if ((start < (3.0 * PI)) && (end > (3.0 * PI)))
+                       if ((start < (TAU + HALF_TAU)) && (end > (TAU + HALF_TAU)))
                                bounds.setLeft(-1.0);
 
-                       if ((start < ((3.0 * PI) + PI_OVER_2)) && (end > ((3.0 * PI) + PI_OVER_2)))
+                       if ((start < (TAU + THREE_QTR_TAU)) && (end > (TAU + THREE_QTR_TAU)))
                                bounds.setBottom(-1.0);
 
                        bounds.setTopLeft(QPointF(bounds.left() * a->radius[0], bounds.top() * a->radius[0]));
@@ -1665,7 +1645,7 @@ bool DrawingView::HitTestObjects(Point point)
 
                        // Make sure we get the angle in the correct spot
                        if (angle < obj->angle[0])
-                               angle += PI_TIMES_2;
+                               angle += TAU;
 
                        // Get the span that we're pointing at...
                        double span = angle - obj->angle[0];
@@ -1756,13 +1736,13 @@ void DrawingView::HandleObjectMovement(Point point)
                                double delta = angle - obj->angle[0];
 
                                if (delta < 0)
-                                       delta += PI_TIMES_2;
+                                       delta += TAU;
 
                                obj->angle[1] -= delta;
                                obj->angle[0] = angle;
 
                                if (obj->angle[1] < 0)
-                                       obj->angle[1] += PI_TIMES_2;
+                                       obj->angle[1] += TAU;
 
                                QString text = QObject::tr("Span: %1") + QChar(0x00B0) + QObject::tr("\n%2") + QChar(0x00B0) + QObject::tr(" - %3") + QChar(0x00B0);
                                informativeText = text.arg(obj->angle[1] * RADIANS_TO_DEGREES, 0, 'd', 4).arg(obj->angle[0] * RADIANS_TO_DEGREES, 0, 'd', 2).arg((obj->angle[0] + obj->angle[1]) * RADIANS_TO_DEGREES, 0, 'd', 2);
@@ -1783,7 +1763,7 @@ void DrawingView::HandleObjectMovement(Point point)
                                obj->angle[1] = angle - obj->angle[0];
 
                                if (obj->angle[1] < 0)
-                                       obj->angle[1] += PI_TIMES_2;
+                                       obj->angle[1] += TAU;
 
                                QString text = QObject::tr("Span: %1") + QChar(0x00B0) + QObject::tr("\n%2") + QChar(0x00B0) + QObject::tr(" - %3") + QChar(0x00B0);
                                informativeText = text.arg(obj->angle[1] * RADIANS_TO_DEGREES, 0, 'd', 4).arg(obj->angle[0] * RADIANS_TO_DEGREES, 0, 'd', 2).arg((obj->angle[0] + obj->angle[1]) * RADIANS_TO_DEGREES, 0, 'd', 2);
@@ -1794,7 +1774,7 @@ void DrawingView::HandleObjectMovement(Point point)
                        obj->angle[0] = angle - obj->angle[1];
 
                        if (obj->angle[0] < 0)
-                               obj->angle[0] += PI_TIMES_2;
+                               obj->angle[0] += TAU;
 
                        QString text = QObject::tr("End angle: %1") + QChar(0x00B0);
                        informativeText = text.arg((obj->angle[0] + obj->angle[1]) * RADIANS_TO_DEGREES, 0, 'd', 4);