-
Notifications
You must be signed in to change notification settings - Fork 1
Line.lua
Pixel edited this page Jul 5, 2018
·
1 revision
Line.lua is a line class library designed in the fashion of Vec and Rec. It requires Vec to work, the same way Rec does. Line is designed to provide basic utilities for line segments, such as intersection, extension, extracting slopes, rotation, etc.
It is instantiated more or less as standard:
local line = Line(0,0,10,10)
local line = Line(Vec(0,0),Vec(10,10))Either of these methods will produce a line segment from (0,0) to (10,10) The components of the line are A and B, respectively.
The line supports the following properties, similarly to the rectangle:
line.typereturns "line" (set)line.xreturns the minimum x coordinate of the line (set)line.x1alias of xline.yreturns the minimum y coordinate of the line (set)line.y1alias of yline.x2returns the maximum x coordinate of the line (set)line.y2returns the maximum y coordinate of the line (set)line.lreturns the leftmost vector of the line (set)line.rreturns the rightmost vector of the line (set)line.ureturns the uppermost vector of the line (set)line.dreturns the bottommost vector of the line (set)line.dxreturns the width of the line (b-a, thus negative if B is left of A) (shifts b)line.dyreturns the height of the line (b-a, thus negative if B is above A) (shifts b)line.mreturns the slope of the line, calculated sensibly (read only)line.yIntreturns the y intercept of the line (moves line)line.mxreturns the middle x coordinate of the line (moves line)line.myreturns the middle y coordinate of the line (moves line)line.midreturns a vector for the middle of the line (moves line)line.anglereturns the angle from A to B (math.atan2(dy,dx)) (read only)line.lengthreturns the distance from A to B (moves B)
The following methods are also supported:
line:solveY(x)returns the y value for the given xline:solveX(y)returns the x value for the given yline:hasX(x)returns true if the line extends through (or ends at) the given xline:hasY(y)returns true if the line extends through (or ends at) the given yline:hasPoint(x,y)returns true if the line includes or ends at the given coordinatesline:isVert()returns true if the line is verticalline:isHoriz()returns true if the line is horizontalline:parallel(line2)returns true if the lines' slopes are equalline:intersectX(line2)Apparently returns the x coordinate of intersection if there is a proper intersection; I have no memory, TODO.line:intersect(line2)returns true if intersecting, then x and y of intersection.line:normal(dir,dist)returns a right normal with a distance of one by default. If dir is 'l', will return a left normal. If dist is supplied, will use that.line:mir(x,y)line:mir(vec)reflects the coordinates about the line. Returns type in kind with input.line:perpA()returns a perpendicular line intersecting on A.line:perpB()returns a perpendicular line intersecting on B.line:perpM()returns a perpendicular line intersecting in the middle.line:projVec(v,getVec)projects the given vector onto the line, returns distance and, if getVec is true, the vector of the projected location.line:projNormA(v,getVec,left)projects onto the right normal of the line (at line.a) by default, left normal if left is true.line:projNormB(v,getVec,left)projects onto the right normal of the line (at line.b) by default, left normal if left is true.line:projNormM(v,getVec,left)projects onto the right normal of the line (at line.mid) by default, left normal if left is true.line:solveDist(v)solves for the point at a distance ofvalong the line, returns the vector location.line:solveNormADist(v,left)solves for the point at a distance ofvalong the A normal of the line - right by default, left when given arg.line:solveNormBDist(v,left)solves for the point at a distance ofvalong the B normal of the line - right by default, left when given arg.line:solveNormMDist(v,left)solves for the point at a distance ofvalong the middle normal of the line - right by default, left when given arg.line:SATPoint(v,left)returns true if the sent vector is behind the line (negative on right normal, unless 'left' is true) and the distance on the projection.line:SATPoints(points,left)returns true if the minimum point is behind the line, then the minimum index and value.pointsshould be an array of vectors.line:SATPointsRec(rectangle,left)returns SAT data of a rectangle's points. Returns intersecting, nearestPointIndex, nearestDistance.line:unpack()returns ax, ay, bx, by.line.fromRec(rec)returns sides of a rectangle - top, right, bottom, left - as lines.line.fromRecI(rec,i)returns theith side of a rectangle as a line.
line:del()recycles the line as above.