Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.

Bug 491404

Summary: Enhancement for Line (getMidPoint)
Product: [Tools] GEF Reporter: Colin Sharples <ctg>
Component: GEF GeometryAssignee: Alexander Nyßen <nyssen>
Status: RESOLVED WORKSFORME QA Contact:
Severity: normal    
Priority: P3 CC: nyssen
Version: 0.2.0   
Target Milestone: ---   
Hardware: PC   
OS: Windows NT   
Whiteboard:
Attachments:
Description Flags
Patch to add getMidPoint method to Line
none
Test case for Line.getMidPoint
none
Re-implemented using get(0.5) none

Description Colin Sharples CLA 2016-04-10 20:42:48 EDT
Created attachment 260834 [details]
Patch to add getMidPoint method to Line

I would like to propose a minor enhancement to org.eclipse.gef4.geometry.planar.Line.

One new method: Point getMidPoint() returns the center point of the line. This is essentially a synonym for Line.getBounds().getCenter(), but is more efficient as it avoids the creation of a Rectangle and two Points.

This contribution complies to the Eclipse Foundation Certificate of Origin.
Comment 1 Colin Sharples CLA 2016-04-11 17:33:09 EDT
Created attachment 260872 [details]
Test case for Line.getMidPoint
Comment 2 Alexander Nyßen CLA 2016-04-12 11:01:27 EDT
Thanks Colin! 

As Line is a BezierCurve, obtaining the mid-point is already possible by using line.get(0.5). Do you propose the enhancement for reasons of convenience or performance? 

In the latter case, I would propose to rather overwrite get(double) within Line to perform the respective optimization (based on your algorithm). In the first case we could think of introducing such a method as a shortcut (for get(0.5)).
Comment 3 Colin Sharples CLA 2016-04-12 15:49:42 EDT
It's a bit of both, really. The center point of a line is a special case, and I have several use cases where you need to access that specific point (e.g. to split a line into two equal halves, to perform a rotation about the center point etc). I see Line.getMidPoint() primarily as an analogy to Rectangle.getCenter() or Polygon.getCentroid().

Given that a straight line is a degenerate case of a BezierCurve, there is certainly a case to be made for optimizing the internals, but I really see that as a separate issue. Note that the implementation I have provided is not (yet) fully optimized, as it is still going to create the line end points. If you prefer to replace the implementation with get(0.5), that's okay with me. That would have the advantage that only the get() method needs to be optimized for performance.
Comment 4 Colin Sharples CLA 2016-04-13 17:51:17 EDT
Created attachment 260940 [details]
Re-implemented using get(0.5)

Replaced implementation using get(0.5), allowing for future performance enhancements to re-implement Line.get(double) in a more efficient manner.

This patch conforms to the Eclipse Foundation Certificate of Origin.

The test case previously submitted is still valid and works with the new implementation.
Comment 5 Alexander Nyßen CLA 2016-05-02 05:38:46 EDT
I re-implemented get(double) within Line to it is more performant then the generic BezierCurve#get(double) method (as it does not longer fall back to it.
Comment 6 Alexander Nyßen CLA 2016-05-02 05:41:26 EDT
As get() covers the requested functionality and as it is now even locally re-implemented, I think an additional alias (getMidPoint()) is not really required (and would again cause a performance-overhead for clients). Resolving thus as WORKSFORME.