For some geometric algorithms it is useful to treat FloatPoint as a vector on the 2D plane and query its length or the square of its length. It is also useful to be able to compute the distance or squared distance between two points. We should add length computations to FloatPoint and FloatSize for these purposes.

Created attachment 66116 [details] Patch From the ChangeLog: Added length and squared length operations to both FloatPoint and FloatSize, and added set(x, y) operation to FloatPoint. These changes have been tested with new code to be added later.

Comment on attachment 66116 [details] Patch ... > Index: WebCore/platform/graphics/FloatPoint.h > =================================================================== > --- WebCore/platform/graphics/FloatPoint.h (revision 66532) > +++ WebCore/platform/graphics/FloatPoint.h (working copy) > @@ -80,6 +80,11 @@ public: > > void setX(float x) { m_x = x; } > void setY(float y) { m_y = y; } > + void set(float x, float y) > + { > + m_x = x; > + m_y = y; > + } > void move(float dx, float dy) > { > m_x += dx; > @@ -91,6 +96,12 @@ public: > m_y *= sy; > } > > + float length() const; > + float lengthSquared() const > + { > + return m_x * m_x + m_y * m_y; > + } > + > #if PLATFORM(CG) > FloatPoint(const CGPoint&); > operator CGPoint() const; > Index: WebCore/platform/graphics/FloatSize.cpp Need a casting operator between FloatPoint and FloatPoint3D (here and in FloatPoint3D). normalize() and dot() would be useful here as well, and would make the classes more consistent. For completeness you could add cross() as well, although I've never seen it used for a 2D point.

(In reply to comment #2) > Need a casting operator between FloatPoint and FloatPoint3D (here and in FloatPoint3D). I don't think this is a good idea. Providing such a casting operator would allow implicit casts and assignments to succeed that could lead to programming errors. FloatPoint3D already has a constructor taking FloatPoint which sets the z component to zero. We could add an xy() method on FloatPoint3D returning FloatPoint (or six methods like xy(), xz(), etc.). > normalize() and dot() would be useful here as well, and would make the classes more consistent. For completeness you could add cross() as well, although I've never seen it used for a 2D point. I can add normalize() and dot(). Cross product is an operation which is only defined on three-dimensional vectors.

Created attachment 66127 [details] Revised patch Added normalize() and dot product operations to FloatPoint based on review feedback.

(In reply to comment #3) > (In reply to comment #2) > > Need a casting operator between FloatPoint and FloatPoint3D (here and in FloatPoint3D). > > I don't think this is a good idea. Providing such a casting operator would allow implicit casts and assignments to succeed that could lead to programming errors. FloatPoint3D already has a constructor taking FloatPoint which sets the z component to zero. We could add an xy() method on FloatPoint3D returning FloatPoint (or six methods like xy(), xz(), etc.). I agree that the ctor is probably sufficient. > > > normalize() and dot() would be useful here as well, and would make the classes more consistent. For completeness you could add cross() as well, although I've never seen it used for a 2D point. > > I can add normalize() and dot(). Cross product is an operation which is only defined on three-dimensional vectors. I have seen 2D cross product implemented. It returns a scalar value which is the magnitude of resultant vector. Apparently (and I never did the math so I can't verify this), it's also twice the area of the triangle formed by the two vectors. But I agree, I don't think this is important. So just normalize and dot will fill out the class nicely.

(In reply to comment #5) > So just normalize and dot will fill out the class nicely. normalize and dot have been added per above. Please re-review.

Committed r66560: <http://trac.webkit.org/changeset/66560>