Path Class Reference

A path is a sequence of lines and curves that may either form a closed shape or be open-ended. More...

List of all members.

Classes

class  Iterator
 Iterates the lines and curves that a path contains. More...

Public Member Functions

 Path ()
 Creates an empty path.
 Path (const Path &other)
 Creates a copy of another path.
 ~Path ()
 Destructor.
Pathoperator= (const Path &other)
 Copies this path from another one.
bool isEmpty () const throw ()
 Returns true if the path doesn't contain any lines or curves.
const Rectangle< float > getBounds () const throw ()
 Returns the smallest rectangle that contains all points within the path.
const Rectangle< float > getBoundsTransformed (const AffineTransform &transform) const throw ()
 Returns the smallest rectangle that contains all points within the path after it's been transformed with the given tranasform matrix.
bool contains (float x, float y, float tolerence=10.0f) const
 Checks whether a point lies within the path.
bool intersectsLine (float x1, float y1, float x2, float y2, float tolerence=10.0f)
 Checks whether a line crosses the path.
void clear () throw ()
 Removes all lines and curves, resetting the path completely.
void startNewSubPath (float startX, float startY)
 Begins a new subpath with a given starting position.
void closeSubPath ()
 Closes a the current sub-path with a line back to its start-point.
void lineTo (float endX, float endY)
 Adds a line from the shape's last position to a new end-point.
void quadraticTo (float controlPointX, float controlPointY, float endPointX, float endPointY)
 Adds a quadratic bezier curve from the shape's last position to a new position.
void cubicTo (float controlPoint1X, float controlPoint1Y, float controlPoint2X, float controlPoint2Y, float endPointX, float endPointY)
 Adds a cubic bezier curve from the shape's last position to a new position.
const Point< float > getCurrentPosition () const
 Returns the last point that was added to the path by one of the drawing methods.
void addRectangle (float x, float y, float width, float height)
 Adds a rectangle to the path.
void addRectangle (const Rectangle< int > &rectangle)
 Adds a rectangle to the path.
void addRoundedRectangle (float x, float y, float width, float height, float cornerSize)
 Adds a rectangle with rounded corners to the path.
void addRoundedRectangle (float x, float y, float width, float height, float cornerSizeX, float cornerSizeY)
 Adds a rectangle with rounded corners to the path.
void addTriangle (float x1, float y1, float x2, float y2, float x3, float y3)
 Adds a triangle to the path.
void addQuadrilateral (float x1, float y1, float x2, float y2, float x3, float y3, float x4, float y4)
 Adds a quadrilateral to the path.
void addEllipse (float x, float y, float width, float height)
 Adds an ellipse to the path.
void addArc (float x, float y, float width, float height, float fromRadians, float toRadians, bool startAsNewSubPath=false)
 Adds an elliptical arc to the current path.
void addCentredArc (float centreX, float centreY, float radiusX, float radiusY, float rotationOfEllipse, float fromRadians, float toRadians, bool startAsNewSubPath=false)
 Adds an arc which is centred at a given point, and can have a rotation specified.
void addPieSegment (float x, float y, float width, float height, float fromRadians, float toRadians, float innerCircleProportionalSize)
 Adds a "pie-chart" shape to the path.
void addLineSegment (float startX, float startY, float endX, float endY, float lineThickness)
 Adds a line with a specified thickness.
void addArrow (float startX, float startY, float endX, float endY, float lineThickness, float arrowheadWidth, float arrowheadLength)
 Adds a line with an arrowhead on the end.
void addStar (float centreX, float centreY, int numberOfPoints, float innerRadius, float outerRadius, float startAngle=0.0f)
 Adds a star shape to the path.
void addBubble (float bodyX, float bodyY, float bodyW, float bodyH, float cornerSize, float arrowTipX, float arrowTipY, int whichSide, float arrowPositionAlongEdgeProportional, float arrowWidth)
 Adds a speech-bubble shape to the path.
void addPath (const Path &pathToAppend)
 Adds another path to this one.
void addPath (const Path &pathToAppend, const AffineTransform &transformToApply)
 Adds another path to this one, transforming it on the way in.
void swapWithPath (Path &other)
 Swaps the contents of this path with another one.
void applyTransform (const AffineTransform &transform) throw ()
 Applies a 2D transform to all the vertices in the path.
void scaleToFit (float x, float y, float width, float height, bool preserveProportions) throw ()
 Rescales this path to make it fit neatly into a given space.
const AffineTransform getTransformToScaleToFit (float x, float y, float width, float height, bool preserveProportions, const Justification &justificationType=Justification::centred) const
 Returns a transform that can be used to rescale the path to fit into a given space.
const Path createPathWithRoundedCorners (float cornerRadius) const
 Creates a version of this path where all sharp corners have been replaced by curves.
void setUsingNonZeroWinding (bool isNonZeroWinding) throw ()
 Changes the winding-rule to be used when filling the path.
bool isUsingNonZeroWinding () const
 Returns the flag that indicates whether the path should use a non-zero winding rule.
void loadPathFromStream (InputStream &source)
 Loads a stored path from a data stream.
void loadPathFromData (const void *data, int numberOfBytes)
 Loads a stored path from a block of data.
void writePathToStream (OutputStream &destination) const
 Stores the path by writing it out to a stream.
const String toString () const
 Creates a string containing a textual representation of this path.
void restoreFromString (const String &stringVersion)
 Restores this path from a string that was created with the toString() method.

Detailed Description

A path is a sequence of lines and curves that may either form a closed shape or be open-ended.

To use a path, you can create an empty one, then add lines and curves to it to create shapes, then it can be rendered by a Graphics context or used for geometric operations.

e.g.

    Path myPath;

    myPath.startNewSubPath (10.0f, 10.0f);          // move the current position to (10, 10)
    myPath.lineTo (100.0f, 200.0f);                 // draw a line from here to (100, 200)
    myPath.quadraticTo (0.0f, 150.0f, 5.0f, 50.0f); // draw a curve that ends at (5, 50)
    myPath.closeSubPath();                          // close the subpath with a line back to (10, 10)

    // add an ellipse as well, which will form a second sub-path within the path..
    myPath.addEllipse (50.0f, 50.0f, 40.0f, 30.0f);

    // double the width of the whole thing..
    myPath.applyTransform (AffineTransform::scale (2.0f, 1.0f));

    // and draw it to a graphics context with a 5-pixel thick outline.
    g.strokePath (myPath, PathStrokeType (5.0f));

A path object can actually contain multiple sub-paths, which may themselves be open or closed.

See also:
PathFlatteningIterator, PathStrokeType, Graphics

Constructor & Destructor Documentation

Path::Path (  ) 

Creates an empty path.

Path::Path ( const Path other  ) 

Creates a copy of another path.

Path::~Path (  ) 

Destructor.


Member Function Documentation

Path& Path::operator= ( const Path other  ) 

Copies this path from another one.

bool Path::isEmpty (  )  const throw ()

Returns true if the path doesn't contain any lines or curves.

const Rectangle<float> Path::getBounds (  )  const throw ()

Returns the smallest rectangle that contains all points within the path.

const Rectangle<float> Path::getBoundsTransformed ( const AffineTransform transform  )  const throw ()

Returns the smallest rectangle that contains all points within the path after it's been transformed with the given tranasform matrix.

bool Path::contains ( float  x,
float  y,
float  tolerence = 10.0f 
) const

Checks whether a point lies within the path.

This is only relevent for closed paths (see closeSubPath()), and may produce false results if used on a path which has open sub-paths.

The path's winding rule is taken into account by this method.

The tolerence parameter is passed to the PathFlatteningIterator that is used to trace the path - for more info about it, see the notes for the PathFlatteningIterator constructor.

See also:
closeSubPath, setUsingNonZeroWinding
bool Path::intersectsLine ( float  x1,
float  y1,
float  x2,
float  y2,
float  tolerence = 10.0f 
)

Checks whether a line crosses the path.

This will return positive if the line crosses any of the paths constituent lines or curves. It doesn't take into account whether the line is inside or outside the path, or whether the path is open or closed.

The tolerence parameter is passed to the PathFlatteningIterator that is used to trace the path - for more info about it, see the notes for the PathFlatteningIterator constructor.

void Path::clear (  )  throw ()

Removes all lines and curves, resetting the path completely.

void Path::startNewSubPath ( float  startX,
float  startY 
)

Begins a new subpath with a given starting position.

This will move the path's current position to the co-ordinates passed in and make it ready to draw lines or curves starting from this position.

After adding whatever lines and curves are needed, you can either close the current sub-path using closeSubPath() or call startNewSubPath() to move to a new sub-path, leaving the old one open-ended.

See also:
lineTo, quadraticTo, cubicTo, closeSubPath
void Path::closeSubPath (  ) 

Closes a the current sub-path with a line back to its start-point.

When creating a closed shape such as a triangle, don't use 3 lineTo() calls - instead use two lineTo() calls, followed by a closeSubPath() to join the final point back to the start.

This ensures that closes shapes are recognised as such, and this is important for tasks like drawing strokes, which needs to know whether to draw end-caps or not.

See also:
startNewSubPath, lineTo, quadraticTo, cubicTo, closeSubPath
void Path::lineTo ( float  endX,
float  endY 
)

Adds a line from the shape's last position to a new end-point.

This will connect the end-point of the last line or curve that was added to a new point, using a straight line.

See the class description for an example of how to add lines and curves to a path.

See also:
startNewSubPath, quadraticTo, cubicTo, closeSubPath
void Path::quadraticTo ( float  controlPointX,
float  controlPointY,
float  endPointX,
float  endPointY 
)

Adds a quadratic bezier curve from the shape's last position to a new position.

This will connect the end-point of the last line or curve that was added to a new point, using a quadratic spline with one control-point.

See the class description for an example of how to add lines and curves to a path.

See also:
startNewSubPath, lineTo, cubicTo, closeSubPath
void Path::cubicTo ( float  controlPoint1X,
float  controlPoint1Y,
float  controlPoint2X,
float  controlPoint2Y,
float  endPointX,
float  endPointY 
)

Adds a cubic bezier curve from the shape's last position to a new position.

This will connect the end-point of the last line or curve that was added to a new point, using a cubic spline with two control-points.

See the class description for an example of how to add lines and curves to a path.

See also:
startNewSubPath, lineTo, quadraticTo, closeSubPath
const Point<float> Path::getCurrentPosition (  )  const

Returns the last point that was added to the path by one of the drawing methods.

void Path::addRectangle ( float  x,
float  y,
float  width,
float  height 
)

Adds a rectangle to the path.

The rectangle is added as a new sub-path. (Any currently open paths will be left open).

See also:
addRoundedRectangle, addTriangle
void Path::addRectangle ( const Rectangle< int > &  rectangle  ) 

Adds a rectangle to the path.

The rectangle is added as a new sub-path. (Any currently open paths will be left open).

See also:
addRoundedRectangle, addTriangle
void Path::addRoundedRectangle ( float  x,
float  y,
float  width,
float  height,
float  cornerSize 
)

Adds a rectangle with rounded corners to the path.

The rectangle is added as a new sub-path. (Any currently open paths will be left open).

See also:
addRectangle, addTriangle
void Path::addRoundedRectangle ( float  x,
float  y,
float  width,
float  height,
float  cornerSizeX,
float  cornerSizeY 
)

Adds a rectangle with rounded corners to the path.

The rectangle is added as a new sub-path. (Any currently open paths will be left open).

See also:
addRectangle, addTriangle
void Path::addTriangle ( float  x1,
float  y1,
float  x2,
float  y2,
float  x3,
float  y3 
)

Adds a triangle to the path.

The triangle is added as a new closed sub-path. (Any currently open paths will be left open).

Note that whether the vertices are specified in clockwise or anticlockwise order will affect how the triangle is filled when it overlaps other shapes (the winding order setting will affect this of course).

void Path::addQuadrilateral ( float  x1,
float  y1,
float  x2,
float  y2,
float  x3,
float  y3,
float  x4,
float  y4 
)

Adds a quadrilateral to the path.

The quad is added as a new closed sub-path. (Any currently open paths will be left open).

Note that whether the vertices are specified in clockwise or anticlockwise order will affect how the quad is filled when it overlaps other shapes (the winding order setting will affect this of course).

void Path::addEllipse ( float  x,
float  y,
float  width,
float  height 
)

Adds an ellipse to the path.

The shape is added as a new sub-path. (Any currently open paths will be left open).

See also:
addArc
void Path::addArc ( float  x,
float  y,
float  width,
float  height,
float  fromRadians,
float  toRadians,
bool  startAsNewSubPath = false 
)

Adds an elliptical arc to the current path.

Note that when specifying the start and end angles, the curve will be drawn either clockwise or anti-clockwise according to whether the end angle is greater than the start. This means that sometimes you may need to use values greater than 2*Pi for the end angle.

Parameters:
x the left-hand edge of the rectangle in which the elliptical outline fits
y the top edge of the rectangle in which the elliptical outline fits
width the width of the rectangle in which the elliptical outline fits
height the height of the rectangle in which the elliptical outline fits
fromRadians the angle (clockwise) in radians at which to start the arc segment (where 0 is the top-centre of the ellipse)
toRadians the angle (clockwise) in radians at which to end the arc segment (where 0 is the top-centre of the ellipse). This angle can be greater than 2*Pi, so for example to draw a curve clockwise from the 9 o'clock position to the 3 o'clock position via 12 o'clock, you'd use 1.5*Pi and 2.5*Pi as the start and finish points.
startAsNewSubPath if true, the arc will begin a new subpath from its starting point; if false, it will be added to the current sub-path, continuing from the current postition
See also:
addCentredArc, arcTo, addPieSegment, addEllipse
void Path::addCentredArc ( float  centreX,
float  centreY,
float  radiusX,
float  radiusY,
float  rotationOfEllipse,
float  fromRadians,
float  toRadians,
bool  startAsNewSubPath = false 
)

Adds an arc which is centred at a given point, and can have a rotation specified.

Note that when specifying the start and end angles, the curve will be drawn either clockwise or anti-clockwise according to whether the end angle is greater than the start. This means that sometimes you may need to use values greater than 2*Pi for the end angle.

Parameters:
centreX the centre x of the ellipse
centreY the centre y of the ellipse
radiusX the horizontal radius of the ellipse
radiusY the vertical radius of the ellipse
rotationOfEllipse an angle by which the whole ellipse should be rotated about its centre, in radians (clockwise)
fromRadians the angle (clockwise) in radians at which to start the arc segment (where 0 is the top-centre of the ellipse)
toRadians the angle (clockwise) in radians at which to end the arc segment (where 0 is the top-centre of the ellipse). This angle can be greater than 2*Pi, so for example to draw a curve clockwise from the 9 o'clock position to the 3 o'clock position via 12 o'clock, you'd use 1.5*Pi and 2.5*Pi as the start and finish points.
startAsNewSubPath if true, the arc will begin a new subpath from its starting point; if false, it will be added to the current sub-path, continuing from the current postition
See also:
addArc, arcTo
void Path::addPieSegment ( float  x,
float  y,
float  width,
float  height,
float  fromRadians,
float  toRadians,
float  innerCircleProportionalSize 
)

Adds a "pie-chart" shape to the path.

The shape is added as a new sub-path. (Any currently open paths will be left open).

Note that when specifying the start and end angles, the curve will be drawn either clockwise or anti-clockwise according to whether the end angle is greater than the start. This means that sometimes you may need to use values greater than 2*Pi for the end angle.

Parameters:
x the left-hand edge of the rectangle in which the elliptical outline fits
y the top edge of the rectangle in which the elliptical outline fits
width the width of the rectangle in which the elliptical outline fits
height the height of the rectangle in which the elliptical outline fits
fromRadians the angle (clockwise) in radians at which to start the arc segment (where 0 is the top-centre of the ellipse)
toRadians the angle (clockwise) in radians at which to end the arc segment (where 0 is the top-centre of the ellipse)
innerCircleProportionalSize if this is > 0, then the pie will be drawn as a curved band around a hollow ellipse at its centre, where this value indicates the inner ellipse's size with respect to the outer one.
See also:
addArc
void Path::addLineSegment ( float  startX,
float  startY,
float  endX,
float  endY,
float  lineThickness 
)

Adds a line with a specified thickness.

The line is added as a new closed sub-path. (Any currently open paths will be left open).

See also:
addArrow
void Path::addArrow ( float  startX,
float  startY,
float  endX,
float  endY,
float  lineThickness,
float  arrowheadWidth,
float  arrowheadLength 
)

Adds a line with an arrowhead on the end.

The arrow is added as a new closed sub-path. (Any currently open paths will be left open).

void Path::addStar ( float  centreX,
float  centreY,
int  numberOfPoints,
float  innerRadius,
float  outerRadius,
float  startAngle = 0.0f 
)

Adds a star shape to the path.

void Path::addBubble ( float  bodyX,
float  bodyY,
float  bodyW,
float  bodyH,
float  cornerSize,
float  arrowTipX,
float  arrowTipY,
int  whichSide,
float  arrowPositionAlongEdgeProportional,
float  arrowWidth 
)

Adds a speech-bubble shape to the path.

Parameters:
bodyX the left of the main body area of the bubble
bodyY the top of the main body area of the bubble
bodyW the width of the main body area of the bubble
bodyH the height of the main body area of the bubble
cornerSize the amount by which to round off the corners of the main body rectangle
arrowTipX the x position that the tip of the arrow should connect to
arrowTipY the y position that the tip of the arrow should connect to
whichSide the side to connect the arrow to: 0 = top, 1 = left, 2 = bottom, 3 = right
arrowPositionAlongEdgeProportional how far along the edge of the main rectangle the arrow's base should be - this is a proportional distance between 0 and 1.0
arrowWidth how wide the base of the arrow should be where it joins the main rectangle
void Path::addPath ( const Path pathToAppend  ) 

Adds another path to this one.

The new path is added as a new sub-path. (Any currently open paths in this path will be left open).

Parameters:
pathToAppend the path to add
void Path::addPath ( const Path pathToAppend,
const AffineTransform transformToApply 
)

Adds another path to this one, transforming it on the way in.

The new path is added as a new sub-path, its points being transformed by the given matrix before being added.

Parameters:
pathToAppend the path to add
transformToApply an optional transform to apply to the incoming vertices
void Path::swapWithPath ( Path other  ) 

Swaps the contents of this path with another one.

The internal data of the two paths is swapped over, so this is much faster than copying it to a temp variable and back.

void Path::applyTransform ( const AffineTransform transform  )  throw ()

Applies a 2D transform to all the vertices in the path.

See also:
AffineTransform, scaleToFit, getTransformToScaleToFit
void Path::scaleToFit ( float  x,
float  y,
float  width,
float  height,
bool  preserveProportions 
) throw ()

Rescales this path to make it fit neatly into a given space.

This is effectively a quick way of calling applyTransform (getTransformToScaleToFit (x, y, w, h, preserveProportions))

Parameters:
x the x position of the rectangle to fit the path inside
y the y position of the rectangle to fit the path inside
width the width of the rectangle to fit the path inside
height the height of the rectangle to fit the path inside
preserveProportions if true, it will fit the path into the space without altering its horizontal/vertical scale ratio; if false, it will distort the path to fill the specified ratio both horizontally and vertically
See also:
applyTransform, getTransformToScaleToFit
const AffineTransform Path::getTransformToScaleToFit ( float  x,
float  y,
float  width,
float  height,
bool  preserveProportions,
const Justification justificationType = Justification::centred 
) const

Returns a transform that can be used to rescale the path to fit into a given space.

Parameters:
x the x position of the rectangle to fit the path inside
y the y position of the rectangle to fit the path inside
width the width of the rectangle to fit the path inside
height the height of the rectangle to fit the path inside
preserveProportions if true, it will fit the path into the space without altering its horizontal/vertical scale ratio; if false, it will distort the path to fill the specified ratio both horizontally and vertically
justificationType if the proportions are preseved, the resultant path may be smaller than the available rectangle, so this describes how it should be positioned within the space.
Returns:
an appropriate transformation
See also:
applyTransform, scaleToFit
const Path Path::createPathWithRoundedCorners ( float  cornerRadius  )  const

Creates a version of this path where all sharp corners have been replaced by curves.

Wherever two lines meet at an angle, this will replace the corner with a curve of the given radius.

void Path::setUsingNonZeroWinding ( bool  isNonZeroWinding  )  throw ()

Changes the winding-rule to be used when filling the path.

If set to true (which is the default), then the path uses a non-zero-winding rule to determine which points are inside the path. If set to false, it uses an alternate-winding rule.

The winding-rule comes into play when areas of the shape overlap other areas, and determines whether the overlapping regions are considered to be inside or outside.

Changing this value just sets a flag - it doesn't affect the contents of the path.

See also:
isUsingNonZeroWinding
bool Path::isUsingNonZeroWinding (  )  const

Returns the flag that indicates whether the path should use a non-zero winding rule.

The default for a new path is true.

See also:
setUsingNonZeroWinding
void Path::loadPathFromStream ( InputStream source  ) 

Loads a stored path from a data stream.

The data in the stream must have been written using writePathToStream().

Note that this will append the stored path to whatever is currently in this path, so you might need to call clear() beforehand.

See also:
loadPathFromData, writePathToStream
void Path::loadPathFromData ( const void *  data,
int  numberOfBytes 
)

Loads a stored path from a block of data.

This is similar to loadPathFromStream(), but just reads from a block of data. Useful if you're including stored shapes in your code as a block of static data.

See also:
loadPathFromStream, writePathToStream
void Path::writePathToStream ( OutputStream destination  )  const

Stores the path by writing it out to a stream.

After writing out a path, you can reload it using loadPathFromStream().

See also:
loadPathFromStream, loadPathFromData
const String Path::toString (  )  const

Creates a string containing a textual representation of this path.

See also:
restoreFromString
void Path::restoreFromString ( const String stringVersion  ) 

Restores this path from a string that was created with the toString() method.

See also:
toString()

The documentation for this class was generated from the following file:
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
Generated on Mon Apr 26 11:42:15 2010 for JUCE by  doxygen 1.6.3