+
+
+/*virtual*/ bool Dimension::HitTest(Point point)
+{
+// Vector orthogonal = Vector::Normal(position, endpoint);
+ Vector orthogonal = Vector::Normal(linePt1, linePt2);
+ // Get our line parallel to our points
+#if 0
+ Point p1 = position + (orthogonal * 10.0 * size);
+ Point p2 = endpoint + (orthogonal * 10.0 * size);
+#else
+ Point p1 = linePt1 + (orthogonal * 10.0 * size);
+ Point p2 = linePt2 + (orthogonal * 10.0 * size);
+#endif
+ Point p3(p1, point);
+
+ hitPoint1 = hitPoint2 = hitLine = hitFlipSwitch = hitChangeSwitch1
+ = hitChangeSwitch2 = false;
+ Vector v1(position, point);
+ Vector v2(endpoint, point);
+// Vector lineSegment(position, endpoint);
+ Vector lineSegment(p1, p2);
+// double t = Geometry::ParameterOfLineAndPoint(position, endpoint, point);
+ double t = Geometry::ParameterOfLineAndPoint(p1, p2, point);
+ double distance;
+ Point midpoint = (p1 + p2) / 2.0;
+ Point hFSPoint = Point(midpoint, point);
+ Point hCS1Point = Point((p1 + midpoint) / 2.0, point);
+ Point hCS2Point = Point((midpoint + p2) / 2.0, point);
+
+ if (t < 0.0)
+ distance = v1.Magnitude();
+ else if (t > 1.0)
+ distance = v2.Magnitude();
+ else
+ // distance = ?Det?(ls, v1) / |ls|
+// distance = fabs((lineSegment.x * v1.y - v1.x * lineSegment.y)
+ distance = fabs((lineSegment.x * p3.y - p3.x * lineSegment.y)
+ / lineSegment.Magnitude());
+
+ if ((v1.Magnitude() * Painter::zoom) < 8.0)
+ hitPoint1 = true;
+ else if ((v2.Magnitude() * Painter::zoom) < 8.0)
+ hitPoint2 = true;
+ else if ((distance * Painter::zoom) < 5.0)
+ hitLine = true;
+
+ if ((hFSPoint.Magnitude() * Painter::zoom) < 8.0)
+ hitFlipSwitch = true;
+ else if ((hCS1Point.Magnitude() * Painter::zoom) < 8.0)
+ hitChangeSwitch1 = true;
+ else if ((hCS2Point.Magnitude() * Painter::zoom) < 8.0)
+ hitChangeSwitch2 = true;
+
+ return (hitPoint1 || hitPoint2 || hitLine || hitFlipSwitch || hitChangeSwitch1 || hitChangeSwitch2 ? true : false);
+}
+
+
+void Dimension::SaveHitState(void)
+{
+ oldHitPoint1 = hitPoint1;
+ oldHitPoint2 = hitPoint2;
+ oldHitLine = hitLine;
+ oldHitFlipSwitch = hitFlipSwitch;
+ oldHitChangeSwitch1 = hitChangeSwitch1;
+ oldHitChangeSwitch2 = hitChangeSwitch2;
+}
+
+
+bool Dimension::HitStateChanged(void)
+{
+ if ((hitPoint1 != oldHitPoint1) || (hitPoint2 != oldHitPoint2)
+ || (hitLine != oldHitLine) || (hitFlipSwitch != oldHitFlipSwitch)
+ || (hitChangeSwitch1 != oldHitChangeSwitch1)
+ || (hitChangeSwitch2 != oldHitChangeSwitch2))
+ return true;
+
+ return false;
+}
+
+
+/*virtual*/ void Dimension::Translate(Vector amount)
+{
+ position += amount;
+ endpoint += amount;
+}
+
+
+/*virtual*/ void Dimension::Rotate(Point point, double angle)
+{
+ Point l1 = Geometry::RotatePointAroundPoint(position, point, angle);
+ Point l2 = Geometry::RotatePointAroundPoint(endpoint, point, angle);
+ position = l1;
+ endpoint = l2;
+}
+
+
+/*virtual*/ void Dimension::Mirror(Point p1, Point p2)
+{
+ Point l1 = Geometry::MirrorPointAroundLine(position, p1, p2);
+ Point l2 = Geometry::MirrorPointAroundLine(endpoint, p1, p2);
+ position = l1;
+ endpoint = l2;
+}
+
+
+/*virtual*/ void Dimension::Save(void)
+{
+ Object::Save();
+ oldEndpoint = endpoint;
+}
+
+
+/*virtual*/ void Dimension::Restore(void)
+{
+ Object::Restore();
+ endpoint = oldEndpoint;
+}
+
+
+/*virtual*/ void Dimension::Enumerate(FILE * file)
+{
+ fprintf(file, "DIMENSION %i (%lf,%lf) (%lf,%lf) %i\n", layer, position.x, position.y, endpoint.x, endpoint.y, dimensionType);
+}
+
+
+/*virtual*/ Object * Dimension::Copy(void)
+{
+#warning "!!! This doesn't take care of attached Dimensions !!!"
+/*
+This is a real problem. While having a pointer in the Dimension to this line's points
+is fast & easy, it creates a huge problem when trying to replicate an object like this.
+
+Maybe a way to fix that then, is to have reference numbers instead of pointers. That
+way, if you copy them, ... you might still have problems. Because you can't be sure if
+a copy will be persistant or not, you then *definitely* do not want them to have the
+same reference number.
+*/
+
+ Dimension * d = new Dimension(position, endpoint, dimensionType, parent);
+ d->size = size;
+ return d;
+}
+
+
+// Dimensions are special: they contain exactly *two* points. Here, we check
+// only for zero/non-zero in returning the correct points.
+/*virtual*/ Vector Dimension::GetPointAtParameter(double parameter)
+{
+ if (parameter == 0)
+ return position;
+
+ return endpoint;
+}
+
+
+/*virtual*/ void Dimension::MovePointAtParameter(double parameter, Vector v)
+{
+ if (parameter == 0)
+ position += v;
+ else if (parameter == 1.0)
+ endpoint += v;
+ else
+ {} // Not sure how to handle this case :-P
+}
+
+#if 0
+/*virtual*/ void Dimension::Connect(Object * obj, double param)
+{
+ // There are four possibilities here...
+ // The param is only looking for 0 or 1 here.
+ if (point1.object == NULL && point2.object == NULL)
+ {
+ point1.object = obj;
+ point1.t = param;
+ }
+ else if (point1.object == NULL && point2.object != NULL)
+ {
+ if (point2.t == param)
+ point2.object = obj;
+ else
+ {
+ point1.object = obj;
+ point1.t = param;
+ }
+ }
+ else if (point1.object != NULL && point2.object == NULL)
+ {
+ if (point1.t == param)
+ point1.object = obj;
+ else
+ {
+ point2.object = obj;
+ point2.t = param;
+ }
+ }
+ else if (point1.object != NULL && point2.object != NULL)
+ {
+ if (point1.t == param)
+ point1.object = obj;
+ else
+ point2.object = obj;
+ }
+}
+
+
+/*virtual*/ void Dimension::Disconnect(Object * obj, double param)
+{
+ if (point1.object == obj && point1.t == param)
+ point1.object = NULL;
+ else if (point2.object == obj && point2.t == param)
+ point2.object = NULL;
+}
+
+
+/*virtual*/ void Dimension::DisconnectAll(Object * obj)
+{
+ if (point1.object == obj)
+ point1.object = NULL;
+
+ if (point2.object == obj)
+ point2.object = NULL;
+}
+#endif
+
+/*virtual*/ QRectF Dimension::Extents(void)
+{
+ Point p1 = position;
+ Point p2 = endpoint;
+
+// if (point1.object)
+// p1 = point1.object->GetPointAtParameter(point1.t);
+//
+// if (point2.object)
+// p2 = point2.object->GetPointAtParameter(point2.t);
+
+ return QRectF(QPointF(p1.x, p1.y), QPointF(p2.x, p2.y));
+}
+
+
+#if 0
+/*virtual*/ ObjectType Dimension::Type(void)
+{
+ return OTDimension;
+}
+#endif
+
+
+void Dimension::FlipSides(void)
+{
+#if 1
+ Vector tmp = position;
+ position = endpoint;
+ endpoint = tmp;
+//Not sure this matters...
+//#warning "!!! May need to swap parameter values on connected objects !!!"
+#else
+ Connection tmp = point1;
+ point1 = point2;
+ point2 = tmp;
+// double tmp = point1.t;
+// point1.t = point2.t;
+// point2.t = tmp;
+// Object * tmp = point1.object;
+// point1.object = point2.object;
+// point2.object = tmp;
+#endif
+ needUpdate = true;
+}
+