3 // Part of the Architektonas Project
4 // (C) 2011 Underground Software
5 // See the README and GPLv3 files for licensing and warranty information
7 // JLH = James Hammons <jlhamm@acm.org>
10 // --- ---------- ------------------------------------------------------------
11 // JLH 03/30/2011 Created this file
12 // JLH 04/03/2011 Added information panel (angles) rendering
13 // JLH 08/16/2013 Added continuous user feedack like for Line and Circle
20 #include "mathconstants.h"
24 Arc::Arc(Vector p1, double r, double a1, double a2, Object * p/*= NULL*/):
25 Object(p1, p), /*type(OTArc),*/ radius(r), startAngle(a1), angleSpan(a2),
26 draggingCenter(false), draggingEdge(false), draggingRotate(false),
28 hitCenter(false), hitArc(false), hitRotate(false), hitSpan(false)
30 // This is in the base class, why can't we use the contructor to fill it???
41 /*virtual*/ void Arc::Draw(Painter * painter)
44 painter->SetPen(QPen(Qt::red, 2.0, Qt::DotLine));
46 Point p1(cos(startAngle), sin(startAngle));
47 Point p2(cos(startAngle + angleSpan), sin(startAngle + angleSpan));
48 Vector handle2 = (p1 * radius) + position;
49 Vector handle3 = (p2 * radius) + position;
51 if ((state == OSSelected) || ((state == OSInactive) && hitRotate))
52 painter->DrawHandle(handle2);
54 if ((state == OSSelected) || ((state == OSInactive) && hitSpan))
55 painter->DrawHandle(handle3);
57 if ((state == OSSelected) || ((state == OSInactive) && hitCenter))
58 painter->DrawHandle(position);
60 if ((state == OSInactive) && !hitArc)
61 painter->SetPen(QPen(Qt::black, 1.0, Qt::SolidLine));
63 painter->DrawArc(position, radius, startAngle, angleSpan);
65 if (draggingRotate || draggingSpan)
69 // If we rotating, we draw a guideline showing the angle we're
71 Point p3(cos(oldAngle), sin(oldAngle));
72 Vector oldLine = (p3 * (radius * 1.25)) + position;
73 pen = QPen(QColor(0x80, 0x80, 0x80), 1.0, Qt::DashLine);
75 // painter->DrawLine((int)position.x, (int)position.y, (int)oldLine.x, (int)oldLine.y);
76 painter->DrawLine(position, oldLine);
79 // In rotating and setting the span, we draw a line showing where
80 // we angle/span is that we're setting.
81 pen = QPen(QColor(0x00, 0xC0, 0x80), 1.0, Qt::DashLine);
83 painter->DrawLine((int)position.x, (int)position.y, (int)oldPoint.x, (int)oldPoint.y);
86 // If we're rotating or setting the span, draw an information panel
87 // showing both absolute and relative angles being set.
88 if (draggingRotate || draggingSpan || draggingEdge)
90 double absAngle = (Vector(oldPoint - position).Angle()) * RADIANS_TO_DEGREES;
91 double relAngle = (startAngle >= oldAngle ? startAngle - oldAngle :
92 startAngle - oldAngle + (2.0 * PI)) * RADIANS_TO_DEGREES;
98 text = QObject::tr("Abs ") + QChar(0x2221) + ": %1" + QChar(0x00B0)
99 + QObject::tr("\nRel ") + QChar(0x2221) + ": %2" + QChar(0x00B0);
100 text = text.arg(absAngle, 0, 'd', 4).arg(relAngle, 0, 'd', 4);
102 else if (draggingSpan)
104 text = QObject::tr("Abs ") + QChar(0x2221) + ": %1" + QChar(0x00B0)
105 + QObject::tr("\nSpan: %2") + QChar(0x00B0);
106 text = text.arg(absAngle, 0, 'd', 4).arg(angleSpan * RADIANS_TO_DEGREES, 0, 'd', 4);
108 else if (draggingEdge)
110 text = QObject::tr("Radius: %1\nScale: %2%");
111 text = text.arg(radius, 0, 'd', 4).arg(radius / oldRadius * 100.0, 0, 'd', 0);
115 pen = QPen(QColor(0x00, 0xFF, 0x00), 1.0, Qt::SolidLine);
116 painter->SetPen(pen);
117 painter->SetBrush(QBrush(QColor(0x40, 0xFF, 0x40, 0x9F)));
118 QRectF textRect(10.0, 10.0, 270.0, 70.0); // x, y, w, h
119 painter->DrawRoundedRect(textRect, 7.0, 7.0);
121 textRect.setLeft(textRect.left() + 14);
122 painter->SetFont(*Object::font);
123 pen = QPen(QColor(0x00, 0x5F, 0xDF));
124 painter->SetPen(pen);
125 painter->DrawText(textRect, Qt::AlignVCenter, text);
127 painter->DrawInformativeText(text);
133 /*virtual*/ Vector Arc::Center(void)
140 We need at least *four* handles for this object:
144 - one for setting the span of the arc
146 We need to think about the intuitive way (if there is any) to grab and
147 manipulate a complex object like this... Need to think, "What should happen when
148 I click here and drag there?"
150 Also: should put the snap logic into the Object base class (as a static method)...
153 /*virtual*/ bool Arc::Collided(Vector point)
155 // Someone told us to fuck off, so we'll fuck off. :-)
159 objectWasDragged = false;
160 bool hitSomething = HitTest(point);
161 draggingCenter = hitCenter;
162 draggingEdge = hitArc;
163 draggingRotate = hitRotate;
164 draggingSpan = hitSpan;
166 // Now that we've done our hit testing on the non-snapped point, snap it if
169 point = SnapPointToGrid(point);
173 We want the arc to go into OSSelected mode if we click on it but don't drag.
174 If we don't click anywhere on the arc, then we want it to go into OSInactive mode.
175 Otherwise, we hit a handle and we want to modify the object whether it's in
176 OSSelected mode or not.
178 If I, go to S. Can drag, but only handles 2, 3, & 4 (since the center is not highlighted
180 However, if dragging, revert to I once finished.
182 If S, stay in S. Can drag all.
184 So, we know this. If wasDragged is true, then there's a chance we need to revert to I.
187 So. We have a matrix that looks like this:
201 so let's do like this:
209 oldAngle = startAngle;
219 /*virtual*/ void Arc::PointerMoved(Vector point)
221 // one other thing to check here for is if a modifier key is being held as well,
222 // to allow for multi-selection
223 if (selectionInProgress)
225 // Check for whether or not the rect contains this circle
226 // if (selection.normalized().contains(Extents()))
227 if (selection.contains(Extents()))
235 // The TLC will send these messages if the object is selected but not clicked on.
236 // So we have to be careful with our assumptions here.
237 // This is actually untrue in that case, we need to come up with something better
239 // objectWasDragged = true;
240 // needUpdate = false;
243 needUpdate = HitStateChanged();
244 objectWasDragged = (draggingCenter | draggingEdge | draggingRotate | draggingSpan);
246 if (objectWasDragged)
248 // if (!(hitHandle1 || hitHandle2 || hitHandle3 || hitHandle4))
251 // Vector delta = point - oldPoint;
255 else if (draggingEdge)
256 radius = Vector::Magnitude(point, position);
257 else if (draggingRotate)
259 startAngle = Vector(point - position).Angle();
261 else if (draggingSpan)
263 double angle = Vector(point - position).Angle();
265 if (angle < startAngle)
268 angleSpan = angle - startAngle;
271 // Why save this? For rendering code?
273 // needUpdate = true;
277 /*virtual*/ void Arc::PointerReleased(void)
279 // Mouse went up, so our dragging is done (if any *was* done, that is)
280 // hitHandle1 = hitHandle2 = hitHandle3 = hitHandle4 = false;
281 draggingCenter = draggingEdge = draggingRotate = draggingSpan = false;
282 hitCenter = hitArc = hitRotate = hitSpan = false;
284 // If the object was dragged, then revert to the old state.
285 // Otherwise, we were probably just clicked, and want to stay in the selected state.
286 if (objectWasDragged)
291 /*virtual*/ bool Arc::HitTest(Point point)
293 hitCenter = hitArc = hitRotate = hitSpan = false;
297 the center of the arc
300 The point on a unit circle given an angle a is x = cos(a), y = sin(a)
301 This vector is already unitized, so all we need to do to get our point is to
302 multiply it by radius (to get the length correct) and add it to the center
303 point (to get the correct position).
305 // Vector v1(point, position); // Head minus tail (vector points at "point")
306 Vector v1(position, point); // Head minus tail (vector points at "point")
307 Point p1(cos(startAngle), sin(startAngle));
308 Point p2(cos(startAngle + angleSpan), sin(startAngle + angleSpan));
309 Vector handle2 = (p1 * radius) + position;
310 Vector handle3 = (p2 * radius) + position;
311 double pointerAngle = v1.Angle();
312 double length = v1.Magnitude();
315 if (v1.Magnitude() < 10.0)
317 else if (Vector(handle3 - point).Magnitude() < 10.0)
319 else if (Vector(handle2 - point).Magnitude() < 10.0)
321 else if ((v1.Magnitude() < radius + 3.0) && (v1.Magnitude() > radius - 3.0)
322 && AngleInArcSpan(pointerAngle))
325 if ((length * Painter::zoom) < 8.0)
327 else if (((fabs(length - radius) * Painter::zoom) < 2.0)
328 && AngleInArcSpan(pointerAngle))
330 else if ((Vector::Magnitude(handle2, point) * Painter::zoom) < 8.0)
332 else if ((Vector::Magnitude(handle3, point) * Painter::zoom) < 8.0)
336 return (hitCenter || hitArc || hitRotate || hitSpan ? true : false);
340 /*virtual*/ QRectF Arc::Extents(void)
342 double start = startAngle;
343 double end = start + angleSpan;
344 QPointF p1(cos(start), sin(start));
345 QPointF p2(cos(end), sin(end));
346 QRectF bounds(p1, p2);
348 // Swap X/Y coordinates if they're backwards...
349 if (bounds.left() > bounds.right())
351 double temp = bounds.left();
352 bounds.setLeft(bounds.right());
353 bounds.setRight(temp);
356 if (bounds.bottom() > bounds.top())
358 double temp = bounds.bottom();
359 bounds.setBottom(bounds.top());
363 // If the end of the arc is before the beginning, add 360 degrees to it
367 // Adjust the bounds depending on which axes are crossed
368 if ((start < PI_OVER_2) && (end > PI_OVER_2))
371 if ((start < PI) && (end > PI))
372 bounds.setLeft(-1.0);
374 if ((start < (PI + PI_OVER_2)) && (end > (PI + PI_OVER_2)))
375 bounds.setBottom(-1.0);
377 if ((start < (2.0 * PI)) && (end > (2.0 * PI)))
378 bounds.setRight(1.0);
380 if ((start < ((2.0 * PI) + PI_OVER_2)) && (end > ((2.0 * PI) + PI_OVER_2)))
383 if ((start < (3.0 * PI)) && (end > (3.0 * PI)))
384 bounds.setLeft(-1.0);
386 if ((start < ((3.0 * PI) + PI_OVER_2)) && (end > ((3.0 * PI) + PI_OVER_2)))
387 bounds.setBottom(-1.0);
389 bounds.setTopLeft(QPointF(bounds.left() * radius, bounds.top() * radius));
390 bounds.setBottomRight(QPointF(bounds.right() * radius, bounds.bottom() * radius));
391 bounds.translate(position.x, position.y);
397 start = 350, span = 20, end = 10, angle = 5
398 angle < start, so angle = 365
400 bool Arc::AngleInArcSpan(double angle)
402 // This should be simple except for a small complication: The start angle plus
403 // the angle span can end up being less than the start angle! So, to check
404 // for this possibility, we check to see if the angle passed in is less than
405 // the start angle and if so, add 2PI to the angle passed in. Then we take the
406 // difference between our start angle and this adjusted angle and see if it
407 // falls within our span.
408 if (angle < startAngle)
411 double passedInSpan = angle - startAngle;
413 return (passedInSpan <= angleSpan ? true : false);
417 void Arc::SaveHitState(void)
419 oldHitCenter = hitCenter;
421 oldHitRotate = hitRotate;
422 oldHitSpan = hitSpan;
426 bool Arc::HitStateChanged(void)
428 if ((hitCenter != oldHitCenter)
429 || (hitArc != oldHitArc)
430 || (hitRotate != oldHitRotate)
431 || (hitSpan != oldHitSpan))
438 /*virtual*/ void Arc::Enumerate(FILE * file)
440 fprintf(file, "ARC %i (%lf,%lf) %lf, %lf, %lf\n", layer, position.x, position.y, radius, startAngle, angleSpan);
444 /*virtual*/ Object * Arc::Copy(void)
446 #warning "!!! This doesn't take care of attached Dimensions !!!"
448 This is a real problem. While having a pointer in the Dimension to this line's points
449 is fast & easy, it creates a huge problem when trying to replicate an object like this.
451 Maybe a way to fix that then, is to have reference numbers instead of pointers. That
452 way, if you copy them, ... you might still have problems. Because you can't be sure if
453 a copy will be persistant or not, you then *definitely* do not want them to have the
454 same reference number.
456 return new Arc(position, radius, startAngle, angleSpan, parent);
460 /*virtual*/ void Arc::Rotate(Point point, double angle)
462 Point c1 = Geometry::RotatePointAroundPoint(position, point, angle);
463 Point ap1(cos(startAngle), sin(startAngle));
464 Point angleStartPoint = (ap1 * radius) + position;
465 Point c2 = Geometry::RotatePointAroundPoint(angleStartPoint, point, angle);
468 startAngle = Vector(c2, c1).Angle();
472 /*virtual*/ void Arc::Mirror(Point p1, Point p2)
474 Point c1 = Geometry::MirrorPointAroundLine(position, p1, p2);
475 Point ap1(cos(startAngle + angleSpan), sin(startAngle + angleSpan));
476 Point angleEndPoint = (ap1 * radius) + position;
477 Point c2 = Geometry::MirrorPointAroundLine(angleEndPoint, p1, p2);
480 startAngle = Vector(c2, c1).Angle();
484 /*virtual*/ void Arc::Save(void)
488 oldStartAngle = startAngle;
489 oldAngleSpan = angleSpan;
493 /*virtual*/ void Arc::Restore(void)
497 startAngle = oldStartAngle;
498 angleSpan = oldAngleSpan;