haplokuon / netDxf

.net dxf Reader-Writer
MIT License
1.01k stars 411 forks source link

Ellipse.cs - PolygonalVertexes method #383

Open artemishenkov opened 2 years ago

artemishenkov commented 2 years ago

Hello, I was working with netDxf library, particularly with the Ellipse class. And I have noticed that the method which converts ellipse to polygonal vertices is working a little bit weird, it's not applying the center position to the resulting list of points. Original code from https://github.com/haplokuon/netDxf/blob/master/netDxf/Entities/Ellipse.cs:

double pointX = 0.5 (this.majorAxis cosAlpha cosBeta - this.minorAxis sinAlpha sinBeta); double pointY = 0.5 (this.majorAxis cosAlpha sinBeta + this.minorAxis sinAlpha cosBeta);

Maybe, it should be changed to something like this: double pointX = 0.5 (this.majorAxis cosAlpha cosBeta - this.minorAxis sinAlpha sinBeta) + Center.X; double pointY = 0.5 (this.majorAxis cosAlpha sinBeta + this.minorAxis sinAlpha cosBeta) + Center.Y;

Thank you!

haplokuon commented 2 years ago

The PolygonalVertexes method of the Ellipse class is correct as it is. This method returns a list of points in local/object coordinate system (OCS), while the ellipse center is defined in world coordinates (WCS), you cannot just add them up. This would only be OK, when the normal vector is the Z Vector, this would be wrong for any other case, you still need to deal with the center Z value. Also take a look that the ellipse rotation is applied since it represents the local rotation of the entity along the local Z axis (normal vector). This behavior is similar as the circle and arc entities.

If you need the vertexes in WCS you need to transform the vertexes returned by the method taking into account the normal vector and the center of the ellipse. Whenever you see a Vector2 as a property or returned by a method of an entity, they are in OCS; if you see a Vector3 it is in WCS; but, I am considering making the ToPolylineVertexes methods to return always a list of Vector3, instead of Vector2, whether they are in object or world coordinate system. In any case the documentation will specify if the points are in OCS or WCS, read it and if you find any kind of discrepancy point me to it.