math::geometry(3tcl)
β¨ NAME β¨
math::geometry β Geometrical computations π
(And yes bestie, this is the math::geometry(3tcl) page from the Tcl Math Library. Buckle up.)
πΈ SYNOPSIS πΈ
package require Tcl ?8.5 9?
package require math::geometry ?1.4.2?
::math::geometry::+ point1 point2
::math::geometry::- point1 point2
::math::geometry::p x y
::math::geometry::distance point1 point2
::math::geometry::length point
::math::geometry::s* factor point
::math::geometry::direction angle
::math::geometry::h length
::math::geometry::v length
::math::geometry::between point1 point2 s
::math::geometry::octant point
::math::geometry::rect nw se
::math::geometry::nwse rect
::math::geometry::angle line
::math::geometry::angleBetween vector1 vector2
::math::geometry::inproduct vector1 vector2
::math::geometry::areaParallellogram vector1 vector2
::math::geometry::calculateDistanceToLine P line
::math::geometry::calculateDistanceToLineSegment P linesegment
::math::geometry::calculateDistanceToPolyline P polyline
::math::geometry::calculateDistanceToPolygon P polygon
::math::geometry::findClosestPointOnLine P line
::math::geometry::findClosestPointOnLineSegment P linesegment
::math::geometry::findClosestPointOnPolyline P polyline
::math::geometry::lengthOfPolyline polyline
::math::geometry::movePointInDirection P direction dist
::math::geometry::lineSegmentsIntersect linesegment1 linesegment2
::math::geometry::findLineSegmentIntersection linesegment1 linesegment2
::math::geometry::findLineIntersection line1 line2
::math::geometry::polylinesIntersect polyline1 polyline2
::math::geometry::polylinesBoundingIntersect polyline1 polyline2 granularity
::math::geometry::intervalsOverlap y1 y2 y3 y4 strict
::math::geometry::rectanglesOverlap P1 P2 Q1 Q2 strict
::math::geometry::bbox polyline
::math::geometry::overlapBBox polyline1 polyline2 ?strict?
::math::geometry::pointInsideBBox bbox point
::math::geometry::cathetusPoint pa pb cathetusLength ?location?
::math::geometry::parallel line offset ?orient?
::math::geometry::unitVector line
::math::geometry::pointInsidePolygon P polyline
::math::geometry::pointInsidePolygonAlt P polyline
::math::geometry::rectangleInsidePolygon P1 P2 polyline
::math::geometry::areaPolygon polygon
::math::geometry::translate vector polyline
::math::geometry::rotate angle polyline
::math::geometry::rotateAbout p angle polyline
::math::geometry::reflect angle polyline
::math::geometry::degToRad angle
::math::geometry::radToDeg angle
::math::geometry::circle centre radius
::math::geometry::circleTwoPoints point1 point2
::math::geometry::pointInsideCircle point circle
::math::geometry::lineIntersectsCircle line circle
::math::geometry::lineSegmentIntersectsCircle segment circle
::math::geometry::intersectionLineWithCircle line circle
::math::geometry::intersectionCircleWithCircle circle1 circle2
::math::geometry::tangentLinesToCircle point circle
::math::geometry::intersectionPolylines polyline1 polyline2 ?mode? ?granularity?
::math::geometry::intersectionPolylineCircle polyline circle ?mode? ?granularity?
::math::geometry::polylineCutOrigin polyline1 polyline2 ?granularity?
::math::geometry::polylineCutEnd polyline1 polyline2 ?granularity?
::math::geometry::splitPolyline polyline numberVertex
::math::geometry::enrichPolyline polyline accuracy
::math::geometry::cleanupPolyline polyline
π¦ DESCRIPTION π¦
okay so the math::geometry package is literally a whole collection of functions for computations and manipulations on two-dimensional geometrical objects, like points, lines, and polygons. β¨
The geometrical objects are implemented as plain lists of coordinates. For instance a line is defined by a list of four numbers, the x- and y-coordinate of a first point and the x- and y-coordinates of a second point on the line.
Note: In version1.4.0an inconsistency was repaired - seehttps://core.tcl-lang.org/tcllib/tktview?name=fb4812f82b. More in COORDINATE SYSTEM.
The various types of object are recognised by the number of coordinate pairs and the context in which they are used: a list of four elements can be regarded as an infinite line, a finite line segment but also as a polyline of one segment and a point set of two points.
Currently the following types of objects are distinguished:
- point β a list of two coordinates representing the x- and y-coordinates respectively.
- line β a list of four coordinates, interpreted as the x- and y-coordinates of two distinct points on the line.
- line segment β a list of four coordinates, interpreted as the x- and y-coordinates of the first and the last points on the line segment.
- polyline β a list of an even number of coordinates, interpreted as the x- and y-coordinates of an ordered set of points.
- polygon β like a polyline, but the implicit assumption is that the polyline is closed (if the first and last points do not coincide, the missing segment is automatically added).
- point set β again a list of an even number of coordinates, but the points are regarded without any ordering.
- circle β a list of three numbers, the first two are the coordinates of the centre and the third is the radius.
π οΈ PROCEDURES π οΈ
The package defines the following public procedures:
::math::geometry::+ point1 point2
Compute the sum of the two vectors given as points and return it. The result is a vector as well. vector math? slay.
::math::geometry::- point1 point2
Compute the difference (point1 - point2) of the two vectors given as points and return it. The result is a vector as well.
::math::geometry::p x y
Construct a point from its coordinates and return it as the result of the command.
::math::geometry::distance point1 point2
Compute the distance between the two points and return it as the result of the command. This is in essence the same as
math::geometry::length [math::geomtry::- point1 point2]
::math::geometry::length point
Compute the length of the vector and return it as the result of the command.
::math::geometry::s* factor point
Scale the vector by the factor and return it as the result of the command. This is a vector as well.
::math::geometry::direction angle
Given the angle in degrees this command computes and returns the unit vector pointing into this direction. The vector for angle == 0 points to the right (east), and for angle == 90 up (north).
::math::geometry::h length
Returns a horizontal vector on the X-axis of the specified length. Positive lengths point to the right (east).
::math::geometry::v length
Returns a vertical vector on the Y-axis of the specified length. Positive lengths point down (south).
::math::geometry::between point1 point2 s
Compute the point which is at relative distance s between the two points and return it as the result of the command. A relative distance of 0 returns point1, the distance 1 returns point2. Distances < 0 or > 1 extrapolate along the line between the two point.
::math::geometry::octant point
Compute the octant of the circle the point is in and return it as the result of the command. The possible results are
- east
- northeast
- north
- northwest
- west
- southwest
- south
- southeast
Each octant is the arc of the circle +/- 22.5 degrees from the cardinal direction the octant is named for.
::math::geometry::rect nw se
Construct a rectangle from its northwest and southeast corners and return it as the result of the command.
::math::geometry::nwse rect
Extract the northwest and southeast corners of the rectangle and return them as the result of the command (a 2-element list containing the points, in the named order).
::math::geometry::angle line
Calculate the angle from the positive x-axis to a given line (in two dimensions only).
list lineβ Coordinates of the line
::math::geometry::angleBetween vector1 vector2
Calculate the angle between two vectors (in degrees)
list vector1β First vectorlist vector2β Second vector
::math::geometry::inproduct vector1 vector2
Calculate the inner product of two vectors
list vector1β First vectorlist vector2β Second vector
::math::geometry::areaParallellogram vector1 vector2
Calculate the area of the parallellogram with the two vectors as its sides
list vector1β First vectorlist vector2β Second vector
::math::geometry::calculateDistanceToLine P line
Calculate the distance of point P to the (infinite) line and return the result
list Pβ List of two numbers, the coordinates of the pointlist lineβ List of four numbers, the coordinates of two points on the line
::math::geometry::calculateDistanceToLineSegment P linesegment
Calculate the distance of point P to the (finite) line segment and return the result.
list Pβ List of two numbers, the coordinates of the pointlist linesegmentβ List of four numbers, the coordinates of the first and last points of the line segment
::math::geometry::calculateDistanceToPolyline P polyline
Calculate the distance of point P to the polyline and return the result. Note that a polyline needs not to be closed.
list Pβ List of two numbers, the coordinates of the pointlist polylineβ List of numbers, the coordinates of the vertices of the polyline
::math::geometry::calculateDistanceToPolygon P polygon
Calculate the distance of point P to the polygon and return the result. If the list of coordinates is not closed (first and last points differ), it is automatically closed.
list Pβ List of two numbers, the coordinates of the pointlist polygonβ List of numbers, the coordinates of the vertices of the polygon::math::geometry::findClosestPointOnLine P lineβ okay so picture this: you've got a point, you've got a whole entire infinite line, and you want the exact spot on that line that's closest to your point. Boom. This hands it to you. πlist Pβ List of two numbers, the coordinates of the pointlist lineβ List of four numbers, the coordinates of two points on the line
::math::geometry::findClosestPointOnLineSegment P linesegmentβ same vibe as above but the line got feelings and also boundaries now, because it's a segment, not an infinite line. Returns the point on the segment closest to your point. πlist Pβ List of two numbers, the coordinates of the pointlist linesegmentβ List of four numbers, the first and last points on the line segment
::math::geometry::findClosestPointOnPolyline P polylineβ the glow-up version: now it's a whole polyline, all those little connected segments, and you still just want the closest point on the whole thing. She'll find it. πΈlist Pβ List of two numbers, the coordinates of the pointlist polylineβ List of numbers, the vertices of the polyline
::math::geometry::lengthOfPolyline polylineβ measures how long the polyline is, like if you walked along every single segment. Important: she is not regarded as a polygon here, so don't come at her with polygon expectations. It's a polyline, periodt.list polylineβ List of numbers, the vertices of the polyline
::math::geometry::movePointInDirection P direction distβ take a point, pick a direction, pick a distance, and she moves. Returns the new coordinates. Two dimensions only, bestie, we stay flat here. π¦list Pβ Coordinates of the point to be moveddouble directionβ Direction (in degrees; 0 is to the right, 90 upwards)list distβ Distance over which to move the point
::math::geometry::lineSegmentsIntersect linesegment1 linesegment2β do these two segments cross, or do they just fully coincide? Returns1if yes,0if no. Two dimensions only. And heads up: if an endpoint of one segment sits on the other segment (or is very close to it), that totally counts as intersecting. πlist linesegment1β First line segmentlist linesegment2β Second line segment
::math::geometry::findLineSegmentIntersection linesegment1 linesegment2β the point where two segments actually meet. Returns the coordinatesβ¦ or the keyword"coincident"if they lie on top of each other, or"none"if they share zero points. Two dimensions only. π«list linesegment1β First line segmentlist linesegment2β Second line segment
::math::geometry::findLineIntersection line1 line2β now with infinite lines, no endpoints, no limits, main character energy. Returns the coordinates of the intersection, or the keyword"coincident"if the lines coincide, or"none"if they have no points in common. Two dimensions only, obviously.list line1β First linelist line2β Second line
Want the actual math tea? The References section has the details on the algorithm and the math behind it. Go look. π
::math::geometry::polylinesIntersect polyline1 polyline2β yes or no, do these two polylines intersect? Two dimensions only.list polyline1β First polylinelist polyline2β Second polyline
::math::geometry::polylinesBoundingIntersect polyline1 polyline2 granularityβ this one's the speedrun version: it checks whether two polylines intersect, but it trades away some correctness down to whatever granularity you give it. Use it when you want faster but weaker intersection checking. πββοΈ
How it works: each polyline gets split into a bunch of smaller polylines, each made of granularity points. If the bounding boxes of any pair of those smaller lines intersect, the procedure returns 1. Otherwise it returns 0.
list polyline1β First polylinelist polyline2β Second polylineint granularityβ Number of points in each part (<=1means check every edge)
::math::geometry::intervalsOverlap y1 y2 y3 y4 strictβ do these two intervals overlap? That's it, that's the whole thing. πdouble y1,y2β Begin and end of first intervaldouble y3,y4β Begin and end of second intervallogical strictβ Check for strict or non-strict overlap
::math::geometry::rectanglesOverlap P1 P2 Q1 Q2 strictβ do these two rectangles overlap? Inquiring minds want to know. β¨list P1β upper-left corner of the first rectanglelist P2β lower-right corner of the first rectanglelist Q1β upper-left corner of the second rectanglelist Q2β lower-right corner of the second rectanglelist strictβ choosing strict or non-strict interpretation
::math::geometry::bbox polylineβ gives you the bounding box of a polyline, returned as a list of four coordinates: the upper-left corner and the lower-right corner of the box. π¦list polylineβ The polyline to be examined
::math::geometry::overlapBBox polyline1 polyline2 ?strict?β do the bounding boxes of these two polylines overlap or not? That's the whole question, bestie.
Arguments:
list polyline1β The first polylinelist polyline1β The second polylineint strictβ Whether strict overlap is to checked (1) or if the bounding boxes may touch (0, default)
Not me noticing the second argument is labelled polyline1 up there π that's literally what the page says, so I'm keeping it exactly as-is. No cap.
::math::geometry::pointInsideBBox bbox pointβ is this point inside the bounding box, or sitting right on it, or neither? Let's find out.
Arguments:
list bboxβ The bounding box given as a list of x/y coordinateslist pointβ The point to be checked
::math::geometry::cathetusPoint pa pb cathetusLength ?location?β okay geometry girlies, gather round: this returns the third point of the rectangular triangle defined by the two given end points of the hypothenusa. The triangle's side from point A (or from point B, if the location is given as"b") to that third point is the cathetus length. And heads up: if the cathetus' length is lower than the length of the hypothenusa, you get back an empty list. That's the rule. π
Arguments:
list paβ The starting point on hypotenuselist pbβ The ending point on hypotenusefloat cathetusLengthβ The length of the cathetus of the trianglestring locationβ The location of the given cathetus,"a"means given cathetus shares pointpa(default)"b"means given cathetus shares pointpb
::math::geometry::parallel line offset ?orient?β hands you a line parallel to the one you gave it, sitting at a distance ofoffset. The orientation is determined by the two points defining the line. π§
Arguments:
list lineβ The given linefloat offsetβ The distance to the given linestring orientβ Orientation of the new line with respect to the given line (defaults to"right")
::math::geometry::unitVector lineβ returns a unit vector from the given line or direction. And plot twist: if thelineargument is a single point, then a line through the origin is assumed. π
Arguments:
list lineβ The line in question (or a single point, implying a line through the origin)
::math::geometry::pointInsidePolygon P polylineβ is this point completely inside a polygon? If the point touches the polygon, then the point is not completely inside the polygon. Boundaries are not it. Nope.list Pβ Coordinates of the pointlist polylineβ The polyline to be examined
::math::geometry::pointInsidePolygonAlt P polylineβ same deal, is the point completely inside the polygon, and again, if the point touches the polygon it is not completely inside. But here's the β¨ twist β¨: this alternative procedure uses the so-called winding number to figure it out, and it handles self-intersecting polygons in a "natural" way. Fancy.list Pβ Coordinates of the pointlist polylineβ The polyline to be examined
::math::geometry::rectangleInsidePolygon P1 P2 polylineβ is this rectangle completely inside a polygon? And if the polygon touches the rectangle, then the rectangle is not complete inside the polygon. Same energy as the point version.list P1β Upper-left corner of the rectanglelist P2β Lower-right corner of the rectanglelist polygonβ The polygon in question
::math::geometry::areaPolygon polygonβ calculates the area of a polygon. That's the whole gig. πlist polygonβ The polygon in question
::math::geometry::translate vector polylineβ slides your whole polyline over by a given vector. π«list vectorβ Translation vectorlist polylineβ The polyline to be translated
::math::geometry::rotate angle polylineβ spins the polyline by a given angle (degrees) around the origin. Yes, the origin specifically.list angleβ Angle over which to rotate the polyline (degrees)list polylineβ The polyline to be rotated
::math::geometry::rotateAbout p angle polylineβ same rotation situation but now you get to pick the pointpshe spins around, and you get the new polyline back. Iconic, honestly.
Arguments:
list pβ The point of rotationfloat angleβ The angle over which to rotate the polyline (degrees)list polylineβ The polyline to be rotated
::math::geometry::reflect angle polylineβ mirrors your polyline in a line that passes through the origin at a given angle (degrees) to the x-axis. πͺlist angleβ Angle of the line of reflection (degrees)list polylineβ The polyline to be reflected
::math::geometry::degToRad angleβ converts degrees to radians. Little guy. Does exactly one job. We love her. πlist angleβ Angle in degrees
::math::geometry::radToDeg angleβ and this one converts radians to degrees. The other direction. Besties. πlist angleβ Angle in radians
::math::geometry::circle centre radiusβ a lil convenience procedure that makes a circle from a point and a radius, so you don't have to assemble it yourself. πlist centreβ Coordinates of the circle centrelist radiusβ Radius of the circle
::math::geometry::circleTwoPoints point1 point2β another convenience one: makes a circle from two points on its circumference. The centre is the point between the two given points, and the radius is half the distance between them. Math? Slayed. βlist point1β (first point on the circumference)list point2β (second point on the circumference)
β¦and finishing off that argument list from the procedure above, bestie π
list point1β First pointlist point2β Second point
::math::geometry::pointInsideCircle point circle
Is this point in the circle or not? π― That's the entire assignment. You get 1 if the point is inside the circle or sitting right on the circumference, and 0 if it's out there living its own life. π
list pointβ Point to be checkedlist circleβ Circle that may or may not contain the point
::math::geometry::lineIntersectsCircle line circle
Does this line meet the circle? 1 if it intersects it or even just grazes it, 0 if it's a total no π.
list lineβ Line to be checkedlist circleβ Circle that may or may not be intersected
::math::geometry::lineSegmentIntersectsCircle segment circle
Same energy as the one above, but for a line segment β so it's finite, it has endpoints, it's a main character with boundaries β¨. 1 = intersects or touches it, 0 = does not.
list segmentβ Line segment to be checkedlist circleβ Circle that may or may not be intersected
::math::geometry::intersectionLineWithCircle line circle
Where exactly does the line cross the circle? π You can get zero, one or two points back. (If the line touches the circle or is close to it, then one point is returned. An arbitrary margin of 1.0e-10 times the radius is used to determine this situation.) So yeah, the math is doing a tiny little "are we touching or not" vibe check with a fuzz factor. π¦
list lineβ Line to be checkedlist circleβ Circle that may or may not be intersected
::math::geometry::intersectionCircleWithCircle circle1 circle2
Two circles, where do they kiss? π Again you can get zero, one or two points. (If the two circles touch the circle or are very close, then one point is returned. An arbitrary margin of 1.0e-10 times the mean of the radii of the two circles is used to determine this situation.)
list circle1β First circlelist circle2β Second circle
::math::geometry::tangentLinesToCircle point circle
Tangent lines from a point to a circle β like, how many lines can you draw from that point that just barely graze the circle? πΈ You can get zero, one or two lines. (If the point is on the circumference or very close to the circle, then one line is returned. An arbitrary margin of 1.0e-10 times the radius of the circle is used to determine this situation.)
list pointβ Point in questionlist circleβ Circle to which the tangent lines are to be determined
::math::geometry::intersectionPolylines polyline1 polyline2 ?mode? ?granularity?
Returns the first point β or all the points β where the two polylines intersect. If the number of points in the polylines is large, you can use the granularity to get an approximate answer faster β‘ (speedrun strats, iykyk).
Arguments:
list polyline1β The first polylinelist polyline2β The second polylinestring modeβ Whether to return only the first (default) or to return all intersection points ("all")int granularityβ The number of points that will be skipped plus 1 in the search for intersection points (1 or smaller means an exact answer is returned)
::math::geometry::intersectionPolylineCircle polyline circle ?mode? ?granularity?
Same situation, but it's a polyline vs a circle now π β returns the first point or all the points where the polyline intersects the circle. Big polyline? Granularity is right there for a faster, approximate answer.
Arguments:
list polylineβ The polyline that may intersect the circlelist circleβ The circle in questionstring modeβ Whether to return only the first (default) or to return all intersection points ("all")int granularityβ The number of points that will be skipped plus 1 in the search for intersection points (1 or smaller means an exact answer is returned)
::math::geometry::polylineCutOrigin polyline1 polyline2 ?granularity?
Scissors out βοΈ the part of the first polyline that runs from the origin up to the first intersection with the second one. And yes β big polyline means granularity is your girl for a faster approximate answer.
Arguments:
list polyline1β The first polyline (from which a part is to be returned)list polyline2β The second polylineint granularityβ The number of points that will be skipped plus 1 in the search for intersection points (1 or smaller means an exact answer is returned)
::math::geometry::polylineCutEnd polyline1 polyline2 ?granularity?
The other blade of the scissors βοΈβ¨ β returns the part of the first polyline going from the last intersection point with the second one all the way to the end. Granularity does its usual speed-up thing when the polyline is huge.
Arguments:
list polyline1β The first polyline (from which a part is to be returned)list polyline2β The second polylineint granularityβ The number of points that will be skipped plus 1 in the search for intersection points (1 or smaller means an exact answer is returned)
::math::geometry::splitPolyline polyline numberVertex
Chops the polyline into a whole set of polylines π° where each separate polyline holds numberVertex vertices between the two end points.
Arguments:
list polylineβ The polyline to be split upint numberVertexβ The number of "internal" vertices
::math::geometry::enrichPolyline polyline accuracy
Takes every segment of a polyline, splits it into a bunch of smaller segments, and returns the glow-up π β¨
Arguments:
list polylineβ The polyline to be refinedint accuracyβ The number of subsegments to be created
::math::geometry::cleanupPolyline polyline
Removes duplicate neighbouring vertices and returns the result π§Ό β decluttering, but make it geometry.
Arguments:
list polylineβ The polyline to be cleaned up
π§ COORDINATE SYSTEM π§
The coordinate system this package lives in is the ordinary cartesian one, bestie: positive x-axis points right, positive y-axis points up. π Angles and directions are measured from the positive x-axis and go in a counter-clockwise direction, so an angle of 90 degrees means the direction of the positive y-axis. Simple, iconic, no notes.
Heads up though π β the Tk canvas coordinates are not this. There the origin sits in the upper left corner of the window. And up to and including version 1.3, the direction and octant procedures of this package used this convention inconsistently. So if you're on old code, be careful. π
π REFERENCES π
- Polygon Intersection [
http:/wiki.tcl.tk/12070] http://en.wikipedia.org/wiki/Line-line_intersectionhttp://local.wasp.uwa.edu.au/~pbourke/geometry/lineline2d/
π BUGS, IDEAS, FEEDBACK π
This document, and the package it describes, will undoubtedly contain bugs and other problems β it's giving "we're all human" π. Please report such in the category math :: geometry of the Tcllib Trackers [http://core.tcl.tk/tcllib/reportlist]. And please also report any ideas for enhancements you may have for either the package and/or the documentation. We want them fr fr π«Ά
When proposing code changes, please provide unified diffs, i.e the output of diff -u.
Note further that attachments are strongly preferred over inlined patches. Attachments can be made by going to the Edit form of the ticket immediately after its creation, and then using the left-most button in the secondary navigation bar. π
π KEYWORDS π
angle, distance, line, math, plane geometry, point
π·οΈ CATEGORY π·οΈ
Mathematics
Β©οΈ COPYRIGHT Β©οΈ
Copyright (c) 2001 by Ideogramic ApS and other parties
Copyright (c) 2010 by Andreas Kupries
Copyright (c) 2010 by Kevin Kenny
Copyright (c) 2018 by Arjen Markus
Copyright (c) 2020 by Manfred Rosenberger
And that's the whole page, babe β geometry, fully understood, no cap. This one ships as part of tcllib, version 1.4.2, and the page you just read is math::geometry(3tcl). Go draw something beautiful with it πβ¨ππ¦
π¦ where this page comes from
- debian trixie: tcllib 2.0+dfsg-4 Β· Tcl
- ubuntu questing: tcllib 2.0+dfsg-4
- ubuntu resolute: tcllib 2.0+dfsg-5 Β· Tcl
- ubuntu stonking: tcllib 2.0+dfsg-6 Β· Tcl
- Copyright (c) 2001 by Ideogramic ApS and other parties
- Copyright (c) 2010 by Andreas Kupries
- Copyright (c) 2010 by Kevin Kenny
- Copyright (c) 2018 by Arjen Markus
- Copyright (c) 2020 by Manfred Rosenberger
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms