class HexaPDF::Content::GraphicObject::Arc

Parent
Included Modules

This class describes an elliptical arc in center parameterization that is approximated using Bezier curves. It can be used to draw circles, circular arcs, ellipses and elliptical arcs, all either in clockwise or counterclockwise direction and optionally inclined in respect to the x-axis.

Note that only the path of the arc itself is added to the canvas. So depending on the use-case the path itself still has to be, for example, stroked.

This graphic object is registered under the :arc key for use with the HexaPDF::Content::Canvas class.

Examples:

arc = canvas.graphic_object(:arc, a: 100, b: 50, end_angle: 150)
canvas.draw(arc).stroke

See: ELL - spaceroots.org/documents/ellipse/elliptical-arc.pdf

Attributes

a[R]

Length of semi-major axis which (without altering the inclination) is parallel to the x-axis, defaults to 1.

Examples:

arc = canvas.graphic_object(:arc, a: 30, b: 30)
canvas.draw(arc).stroke
canvas.stroke_color("hp-blue").draw(arc, a: 60).stroke

b[R]

Length of semi-minor axis which (without altering the inclination) is parallel to the y-axis, defaults to 1.

Examples:

arc = canvas.graphic_object(:arc, a: 30, b: 30)
canvas.draw(arc).stroke
canvas.stroke_color("hp-blue").draw(arc, b: 60).stroke

clockwise[R]

Direction of arc - if true in clockwise direction, else in counterclockwise direction (the default).

This is needed when filling paths using the nonzero winding number rule to achieve different effects.

Examples:

arc = canvas.graphic_object(:arc, a: 40, b: 40)
canvas.fill_color("hp-blue").
  draw(arc, cx: -50).draw(arc, cx: 50).
  draw(arc, cx: -50, b: 80).
  draw(arc, cx: 50, b: 80, clockwise: true).
  fill(:nonzero)

cx[R]

x-coordinate of center point, defaults to 0.

Examples:

arc = canvas.graphic_object(:arc, a: 30, b: 20)
canvas.draw(arc).stroke
canvas.stroke_color("hp-blue").draw(arc, cx: 50).stroke

cy[R]

y-coordinate of center point, defaults to 0.

Examples:

arc = canvas.graphic_object(:arc, a: 30, b: 20)
canvas.draw(arc).stroke
canvas.stroke_color("hp-blue").draw(arc, cy: 50).stroke

end_angle[R]

End angle of the arc in degrees, defaults to 0.

Examples:

arc = canvas.graphic_object(:arc, a: 30, b: 30)
canvas.draw(arc, end_angle: 160).stroke

inclination[R]

Inclination in degrees of the semi-major axis with respect to the x-axis, defaults to 0.

Examples:

arc = canvas.graphic_object(:arc, a: 60, b: 30)
canvas.draw(arc, inclination: 45).stroke

max_curves[RW]

The maximal number of curves used for approximating a complete ellipse.

The higher the value the better the approximation will be but it will also take longer to compute. The value should not be lower than 4. Default value is 6 which already provides a good approximation.

Examples:

arc = canvas.graphic_object(:arc, cx: -50, a: 40, b: 40, max_curves: 2)
canvas.draw(arc)
canvas.draw(arc, cx: 50, max_curves: 10)
canvas.stroke

start_angle[R]

Start angle of the arc in degrees, defaults to 0.

Examples:

arc = canvas.graphic_object(:arc, a: 30, b: 30)
canvas.draw(arc, start_angle: 110).stroke

Public Class Methods

configure(**kwargs)

Creates and configures a new elliptical arc object.

See configure for the allowed keyword arguments.

new()

Creates an elliptical arc with default values (a counterclockwise unit circle at the origin).

Examples:

canvas.draw(:arc).stroke

Public Instance Methods

configure(cx: nil, cy: nil, a: nil, b: nil, start_angle: nil, end_angle: nil, inclination: nil, clockwise: nil, max_curves: nil)

Configures the arc with

  • center point (cx, cy),

  • semi-major axis a,

  • semi-minor axis b,

  • start angle of start_angle degrees,

  • end angle of end_angle degrees and

  • an inclination in respect to the x-axis of inclination degrees,

  • as well as the maximal number of curves max_curves used for approximation.

The clockwise argument determines if the arc is drawn in the counterclockwise direction (false) or in the clockwise direction (true).

For the meaning of max_curves see the description of max_curves.

Any arguments not specified are not modified and retain their old value, see initialize for the inital values.

Returns self.

Examples:

arc = canvas.graphic_object(:arc)
arc.configure(cx: 50, a: 40, b: 20, inclination: 10)
canvas.draw(arc).stroke

curves()

Returns an array of arrays that contain the points for the Bezier curves which are used for approximating the elliptical arc between start_point and end_point.

One subarray consists of

[end_point_x, end_point_y, p1: control_point_1, p2: control_point_2]

The first start point is the one returned by start_point, the other start points are the end points of the curve before.

The format of the subarray is chosen so that it can be fed to the Canvas#curve_to method by using array splatting.

See: ELL s3.4.1 (especially the last box on page 18)

draw(canvas, move_to_start: true)

Draws the arc on the given Canvas.

If the argument move_to_start is true, a Canvas#move_to operation is executed to move the current point to the start point of the arc. Otherwise it is assumed that the current point already coincides with the start point. This functionality is used, for example, by the SolidArc implementation.

The max_curves value, if not already changed, is set to the value of the configuration option ‘graphic_object.arc.max_curves’ before drawing.

Examples:

arc = canvas.graphic_object(:arc, a: 40, b: 30)
canvas.stroke_color("hp-blue").move_to(-50, 0)
arc.draw(canvas, move_to_start: false)
canvas.stroke

end_point()

Returns the end point of the elliptical arc.

Examples:

arc = canvas.graphic_object(:arc, a: 40, b: 30, end_angle: 245)
canvas.draw(arc).stroke
canvas.fill_color("hp-blue").circle(*arc.end_point, 2).fill

point_at(angle)

Returns the point at angle degrees on the ellipse.

Note that the point may not lie on the arc itself!

Examples:

arc = canvas.graphic_object(:arc, a: 40, b: 30, end_angle: 245)
canvas.draw(arc).stroke
canvas.fill_color("hp-blue").
  circle(*arc.point_at(150), 2).
  circle(*arc.point_at(290), 2).
  fill

start_point()

Returns the start point of the elliptical arc.

Examples:

arc = canvas.graphic_object(:arc, a: 40, b: 30, start_angle: 60)
canvas.draw(arc).stroke
canvas.fill_color("hp-blue").circle(*arc.start_point, 2).fill