This is a plugin for the svg.js library to which provides common math functions.
svg.math.js is licensed under the terms of the MIT License.
Include this plugin after including svg.js in your html document.
The math library is a utility library to aid common geometric calculations when dealing with SVG:s. The Library is splitted into seperate functions and a point and line object. All angles in the library is using radians.
Calculate the angle of a line, or the angle between three points.
var angle = SVG.math.angle(p1, p2[, p3])
- p1 - (point) The first point
- p2 - (point) The second point
- p3 - (point, optional) The third point
- (numeric) A numeric value between 0 and 2*pi
Convert an angle in degrees to radians.
var angle = SVG.math.rad(degree)
- degree - (numeric) angle in degrees
- (numeric) An angle in radians
Convert an angle in radians to degrees.
var angle = SVG.math.deg(radians)
- radians - (numeric) angle in radians
- (numeric) An angle in degrees
Round to angle to the nerest angle in the directions array
var angle = SVG.math.snapToAngle(angle, [0, Math.PI/2, Math.PI, Math.PI*3/2])
- angle - (numeric) angle in radians
- directions - (array) array containing angles
- (numeric) An angle in degrees
Linear interpolation between a and b where 0 <= x <= 1.
var angle = SVG.math.lerp(a, b, x)
- a - (numeric) The first floating point value.
- b - (numeric) The second floting point value.
- x - (numeric) A value that linearly interpolates between the x parameter and the y parameter.
- (numeric) An angle in degrees
The linear interpolation is based on the formula a + x * (b - a)
The point object is used in the math library to store x and y coordinates.
var point = new SVG.math.Point(x, y);
- x - (numeric) The x coordinate of the Point.
- y - (numeric) The y coordinate of the Point.
- (point) A new SVG.math.Point object.
// Creating an new point object
var point = new SVG.math.Point(10, 10);
The draw function is a utility function mainly for debugging. It will draw a circle on the SVG in the first argument.
var point = new SVG.math.Point(x, y).draw(svg);
- svg - (numeric) The SVG to draw on. If null it will remove the itself from SVG.
- options - (object, optional) Options for the circle, takes same attributes as a normal circle.
- (Point) It will return itself.
The point object is used in the math library to both represent a line segment and a line.
The constructor takes to point objects which will define both a line and a line segment.
If a function, for instance on the form y = mx + a
should be modeled with this object
your should just calculate two points from that function, see third example.
var line = new SVG.math.Line(p1, p2);
- p1 - (Point) The firts point that defines the line
- p2 - (Point) The second point that defines the line
- (line) A new SVG.math.Line object.
// Creating an new line object
var line = new SVG.math.Line(
new SVG.math.Point(10, 10),
new SVG.math.Point(20, 20);
);
// Creating an new line by using a custom point object
var line = new SVG.math.Line(
{x: 10, y:10},
{x: 20, y:20}
);
// Creating a line object from a *function* on the form `y = mx + a`
var func = new SVG.math.Line(
{x: 1, y: m + a},
{x: 10, y: 10*m + a}
);
The draw function is a utility function mainly for debugging. It will draw a line on the SVG in the first argument.
var line = new SVG.math.Line(x, y).draw(svg);
- svg - (numeric) The SVG to draw on. If null it will remove the itself from SVG.
- options - (object, optional) Options for the line, takes same attributes as a normal line.
- (Line) It will return itself.
Updates the points that defines the line.
If the line has been drawn to a SVG it should also be updated.
line.update(x1, x2);
- p1 - (Point) The first point defining the line.
- p2 - (Point) The second point defining the line.
- (Line) It will return itself.
Returns true if the lines are parallel.
var isParallel = line.update(line2);
- line - (Line) The line to check if it's parallel.
- (boolean) True if the lines are parallel, false otherwise.
Move a distance on the line from the point from towards the point towards. See figure for more information.
line.move(from, towards, distance);
- from - (Point) The Point on the line to move from
- towards - (Point) A Point on the line to move towards
- distance - (numeric) The distance to move
- (Point) A Point on the line.
Returns an the intersection point between the two lines. There is an additional property
parallel
in the Point object which will be true if the two lines are parallel, false otherwise.
line.intersection(line2);
- line - (Line) The line to find the intersection point with.
- (Point) A special point object representing the intersection of the two lines.
Returns the midpoint of the line segment.
line.midPoint();
- (Point) The midpoint of the line segment.
Retutns the squared segment length.
line.segmentLengthSquared();
- (numeric) The squared segment length.
Calculatates the interpolation (x
) value of the closest point
on the line to point
. This is mainly used internally, it is probably
the method closestPoint
you want.
var interpolation = line.closestLinearInterpolation(point);
- point - (Point) The point to find the interpolation value for.
- (numeric) The interpolatioin value.
Calculates, with a linear interpolation, a new point. This is mainly used internally, when finding closest point, midpoint or similar calculations.
var point = line.interpolatedPoint(t);
- t - (numeric) The interpolation value.
- (Point) A point of calculated with a numeric interpolation.
Calculates the closest point on the line with p
.
var point = line.closestPoint(p);
- p - (Point) The point to find the closest point on the line to.
- (Point) A point of calculated with a numeric interpolation.
Calculates a perpendicular line that passes through the point p
and has the
length distance
. The midpoint of the new line will be p
.
var line2 = line.perpendicularLine(p, distance);
- p - (Point) The midpoint of the new line.
- distance - (numeric) The length of the new line.
- (Line) A new Line object which the midpoint
p
and the lengthdistance
.
This example will show how to draw an arrow. Please note that this is not the fastest way to draw this shape, but it's only an example how it's possible to utilize the functions in the math library.
-
Create a line object that defines the start and end of the arrow.
var shadowPath = new SVG.math.Line(p1, p2);
-
Now we want to find the back point of the arrow (p3 in the figure). the easiest way to do this is to use the move function.
var p3 = shadowPath.move(shadowPath.p2, shadowPath.p1, distance);
-
Now we will calculate the body of the arrow. We will do that by calculate the perpedicular lines through the points
p1
andp3
. This will calculate the green point on the black dashed lines.var back = shadowPath.perpendicularLine(shadowPath.p1, arrowThickness); var front = shadowPath.perpendicularLine(shadowPath.p2, arrowThickness);
-
Almost there, the only thing missing is the last points on the arrow head. That is, the green points on the red dashed line. We will calculate those points in a similar fashion as the last step.
var arrowhead = shadowPath.perpendicularLine(shadowPath.p2, arrowheadWidth);
-
The only thing left now is to draw the arrow.
var draw = SVG('paper'); draw.polygon( [back.p1.x, back.p1.y], [front.p1.x, front.p1.y], [arrhowhead.p1.x, arrowhead.p1.y], [p2.x, p2.y], [arrhowhead.p2.x, arrowhead.p2.y], [front.p2.x, front.p2.y], [back.p2.x, back.p2.y] );