math::geometry(n)
π math::geometry β bestie edition π
okay so you found math::geometry(n) from the Tcl Math Library and honestly? iconic. we're doing 2D geometry today and we're gonna slay it together.
β¨ NAME β¨
math::geometry β Geometrical computations πΈ
π¦ 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 π
math::geometry is literally just a big pile of functions for doing computations and manipulations on two-dimensional geometrical objects. we're talking points, lines, polygons. the whole squad. π
the way this works is that all the geometrical objects are implemented as plain lists of coordinates. no fancy structs, no classes, just lists. so for example a line is a list of four numbers: the x- and y-coordinate of a first point, and the x- and y-coordinate of a second point on that line. that's it. that's the line. β¨
Note: in version 1.4.0 an inconsistency was repaired β see https://core.tcl-lang.org/tcllib/tktview?name=fb4812f82b. more deets down in COORDINATE SYSTEM.
so the different types of object get recognised by how many coordinate pairs there are and the context they're used in. like a list of four elements could be an infinite line, could be a finite line segment, could be a polyline of one segment, could be a point set of two points. it's giving multitasking queen. π
currently these are the object types we distinguish, periodt:
- point β a list of two coordinates, representing the x- and y-coordinates respectively. π
- line β a list of four coordinates, read as the x- and y-coordinates of two distinct points on the line. π¦
- line segment β a list of four coordinates, read 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, read 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 (and if the first and last points don't coincide, the missing segment is automatically added for you, no worries bestie). π
- point set β again a list of an even number of coordinates, but the points are regarded as having no ordering whatsoever. chaos. love that for them. π
- 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 these public procedures, and we're going through every single one because I refuse to skip. π
π§ the basics
::math::geometry::+ point1 point2β adds the two vectors (given as points) together and returns the sum. yes it's a vector too. math ate. π₯::math::geometry::- point1 point2β computes the difference(point1 - point2)of the two vectors given as points and returns it. also a vector. π::math::geometry::p x yβ builds a point out of its coordinates and hands it back to you as the result of the command. tiny little constructor, so cute. π::math::geometry::distance point1 point2β computes the distance between the two points and returns it. essentially this is the exact same thing as:
math::geometry::length [math::geomtry::- point1 point2]
heads up: yes, that's <yellow>math::geomtry::-</yellow> up there and no, I'm not fixing it β that's what the page says, character for character. copy it exactly if you're copying it. π
::math::geometry::length pointβ computes the length of the vector and returns it as the result of the command. πΈ::math::geometry::s* factor pointβ scales the vector by the factor and returns it. this is a vector as well, obviously. π
π¦ directions and vectors
::math::geometry::direction angleβ give it an angle in degrees and it computes and returns the unit vector pointing in that direction. the vector forangle == 0points to the right (east), and forangle == 90it points up (north). compass queen. π§::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). β¬οΈ yes, down. don't come for me, that's just the vibe.::math::geometry::between point1 point2 sβ computes the point that's at relative distancesbetween the two points and returns it. a relative distance of0returnspoint1, the distance1returnspoint2. and distances< 0or> 1extrapolate along the line between the two points β so yes you can absolutely go feral and run off the end. π::math::geometry::octant pointβ computes which octant of the circle the point is in and returns it. 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. eight slices of pie, each one labelled. π₯§
π rectangles and angles
::math::geometry::rect nw seβ constructs a rectangle from its northwest and southeast corners and returns it as the result of the command. π«Ά::math::geometry::nwse rectβ extracts the northwest and southeast corners of the rectangle and returns them (a 2-element list containing the points, in the named order). reverse uno card. π::math::geometry::angle lineβ calculates the angle from the positive x-axis to a given line (in two dimensions only, bestie, we are not doing 3D today).- list line β coordinates of the line
::math::geometry::angleBetween vector1 vector2β calculates the angle between two vectors (in degrees). π- list vector1 β first vector
- list vector2 β second vector
::math::geometry::inproduct vector1 vector2β calculates the inner product of two vectors.- list vector1 β first vector
- list vector2 β second vector
::math::geometry::areaParallellogram vector1 vector2β calculates the area of the parallellogram with the two vectors as its sides. (yes, "parallellogram" with the double L, that's the spelling on the page, we respect it.) π- list vector1 β first vector
- list vector2 β second vector
π distances to stuff
::math::geometry::calculateDistanceToLine P lineβ calculates the distance of pointPto the (infinite) line and returns the result. infinite. no ends. endless. main character energy. β¨- list P β list of two numbers, the coordinates of the point
- list line β list of four numbers, the coordinates of two points on the line
::math::geometry::calculateDistanceToLineSegment P linesegmentβ calculates the distance of pointPto the (finite) line segment and returns the result. this one has boundaries, it has limits, it's setting healthy boundaries bestie. π§- list P β list of two numbers, the coordinates of the point
- list linesegment β list of four numbers, the coordinates of the first and last points of the line segment
::math::geometry::calculateDistanceToPolyline P polylineβ calculates the distance of pointPto the polyline and returns the result. note that a polyline needs not to be closed. it's open, it's free, it's living its truth. π¦- list P β list of two numbers, the coordinates of the point
- list polyline β list of numbers, the coordinates of the vertices of the polyline
::math::geometry::calculateDistanceToPolygon P polygonβ calculates the distance of pointPto the polygon and returns the result. if the list of coordinates is not closed (first and last points differ), it is automatically closed for you. the package is a considerate friend. π«Ά- list P β list of two numbers, the coordinates of the point
- list polygon β list of numbers, the coordinates of the vertices of the polygon
okay that's the first chunk, there's more coming, don't go anywhere π
::math::geometry::findClosestPointOnLine P line
Okay so this one returns the point on a line that's closest to a given point. Iconic.
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
Returns the point on a line segment that's closest to a given point. Not the whole infinite line, just the segment, bestie.
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
Returns the point on a polyline that's closest to a given point. β¨
list Pβ List of two numbers, the coordinates of the pointlist polylineβ List of numbers, the vertices of the polyline
::math::geometry::lengthOfPolyline polyline
Returns the length of the polyline. Note: it is not regarded as a polygon. Just a lil' open path, okay?
list polylineβ List of numbers, the vertices of the polyline
::math::geometry::movePointInDirection P direction dist
Move a point over a given distance in a given direction and return the new coordinates. (Two dimensions only, bestie.)
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
Check if two line segments intersect or coincide. Returns 1 if that's the case, 0 otherwise. (Two dimensions only.) If an endpoint of one segment lies on the other segment (or is very close to the segment), they are considered to intersect. So like, touching counts.
list linesegment1β First line segmentlist linesegment2β Second line segment
::math::geometry::findLineSegmentIntersection linesegment1 linesegment2
Find the intersection point of two line segments. Returns the coordinates or the keywords "coincident" or "none" if the line segments coincide or have no points in common. (Two dimensions only.)
list linesegment1β First line segmentlist linesegment2β Second line segment
::math::geometry::findLineIntersection line1 line2
Find the intersection point of two (infinite) lines. Returns the coordinates or the keywords "coincident" or "none" if the lines coincide or have no points in common. (Two dimensions only.) See section References for details on the algorithm and math behind it. iykyk
list line1β First linelist line2β Second line
::math::geometry::polylinesIntersect polyline1 polyline2
Check if two polylines intersect or not. (Two dimensions only.)
list polyline1β First polylinelist polyline2β Second polyline
::math::geometry::polylinesBoundingIntersect polyline1 polyline2 granularity
Check whether two polylines intersect, but reduce the correctness of the result to the given granularity. Use this for faster, but weaker, intersection checking.
How it works: Each polyline is split into a number of smaller polylines, consisting of granularity points each. If a pair of those smaller lines' bounding boxes intersect, then this procedure returns 1, otherwise it returns 0. So it's a vibe check, not a full audit.
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
Check if two intervals overlap. Overlap core.
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
Check if two rectangles overlap. Rectangle drama.
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
Note: original says list strict, even though it sounds like a logical. We keep it exactly. No cap.
::math::geometry::bbox polyline
Calculate the bounding box of a polyline. Returns a list of four coordinates: the upper-left and the lower-right corner of the box. Boxed in, but make it fashion.
list polylineβ The polyline to be examined
::math::geometry::overlapBBox polyline1 polyline2 ?strict?
Check if the bounding boxes of two polylines overlap or not.
Arguments:
list polyline1β The first polylinelist polyline1β The second polyline (yes, the original sayspolyline1again, we don't make the rules π)int strictβ Whether strict overlap is to checked (1) or if the bounding boxes may touch (0, default)
::math::geometry::pointInsideBBox bbox point
Check if the point is inside or on the bounding box or not.
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?
Return 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 B, if the location is given as "b") to the third point is the cathetus length. If the cathetus' length is lower than the length of the hypothenusa, an empty list is returned. Weird but okay.
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?
Return a line parallel to the given line, with a distance "offset". 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
Return a unit vector from the given line or direction, if the line argument 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
Determine if a point is completely inside a polygon. If the point touches the polygon, then the point is not completely inside the polygon. So touching means you're out. Boundaries are strict, bestie.
list Pβ Coordinates of the pointlist polylineβ The polyline to be examined
::math::geometry::pointInsidePolygonAlt P polyline
Determine if a point is completely inside a polygon. If the point touches the polygon, then the point is not completely inside the polygon. Note: this alternative procedure uses the so-called winding number to determine this. 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
Determine if a rectangle is completely inside a polygon. If polygon touches the rectangle, then the rectangle is not complete inside the polygon. (Yes, it says "complete" not "completely" β we keep it.)
list P1β Upper-left corner of the rectanglelist P2β Lower-right corner of the rectanglelist polygonβ The polygon in question
::math::geometry::areaPolygon polygon
Calculate the area of a polygon. Math girlies, rise.
list polygonβ The polygon in question
::math::geometry::translate vector polyline
Translate a polyline over a given vector. Slide to the left, slide to the right.
list vectorβ Translation vectorlist polylineβ The polyline to be translated
::math::geometry::rotate angle polyline
Rotate a polyline over a given angle (degrees) around the origin. Spin it.
list angleβ Angle over which to rotate the polyline (degrees)list polylineβ The polyline to be rotated
::math::geometry::rotateAbout p angle polyline
Rotate a polyline around a given point p and return the new polyline.
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
Reflect a polyline in a line through the origin at a given angle (degrees) to the x-axis. Mirror, mirror.
list angleβ Angle of the line of reflection (degrees)list polylineβ The polyline to be reflected
::math::geometry::degToRad angle
Convert from degrees to radians. Because radians are the main character.
list angleβ Angle in degrees
::math::geometry::radToDeg angle
Convert from radians to degrees. And back again.
list angleβ Angle in radians
::math::geometry::circle centre radius
Convenience procedure to create a circle from a point and a radius. Circle slay.
list centreβ Coordinates of the circle centrelist radiusβ Radius of the circle
::math::geometry::circleTwoPoints point1 point2
Convenience procedure to create a circle from two points on its circumference. The centre is the point between the two given points, the radius is half the distance between them. Geometry matchmaking, fr.
point1(list) β First pointpoint2(list) β Second point
::math::geometry::pointInsideCircle point circle
Okay so this one is your vibe check for a point vs a circle. It tells you if the given point is inside the circle or on the circumference β that's a 1 β or outside β that's a 0. no cap.
Arguments:
point(list) β Point to be checkedcircle(list) β Circle that may or may not contain the point
::math::geometry::lineIntersectsCircle line circle
Does the line hit the circle or just graze it? That's a 1. Does it completely miss? That's a 0. it's giving intersection energy.
Arguments:
line(list) β Line to be checkedcircle(list) β Circle that may or may not be intersected
::math::geometry::lineSegmentIntersectsCircle segment circle
Same vibe as above but for a line segment instead of an infinite line. Returns 1 if it intersects or touches the circle, 0 if not.
Arguments:
segment(list) β Line segment to be checkedcircle(list) β Circle that may or may not be intersected
::math::geometry::intersectionLineWithCircle line circle
Bestie, this one finds the actual points where the line meets the circle. There can be zero, one, or two points. If the line touches the circle or is super close to it, then you get one point back. The package uses an arbitrary margin of 1.0e-10 times the radius to decide if that's the situation. slay.
Arguments:
line(list) β Line to be checkedcircle(list) β Circle that may or may not be intersected
::math::geometry::intersectionCircleWithCircle circle1 circle2
Two circles, one moment. This returns the points where they intersect. Again, zero, one, or two points possible. If they touch or are very close, you get one point. The arbitrary margin here is 1.0e-10 times the mean of the radii of the two circles. iykyk.
Arguments:
circle1(list) β First circlecircle2(list) β Second circle
::math::geometry::tangentLinesToCircle point circle
Finding tangent lines from a point to a circle. There can be zero, one, or two lines. If the point is on the cirucmference or very close to the circle, then one line is returned. The arbitrary margin is 1.0e-10 times the radius of the circle. (Yes, cirucmference is spelled like that in the original, and we're keeping it π.)
Arguments:
point(list) β Point in questioncircle(list) β Circle to which the tangent lines are to be determined
::math::geometry::intersectionPolylines polyline1 polyline2 ?mode? ?granularity?
Returns the first point or all points where two polylines intersect. If your polylines have a lot of points, you can use the granularity to get an approximate answer faster. main character energy.
Arguments:
polyline1(list) β The first polylinepolyline2(list) β The second polylinemode(string) β Whether to return only the first (default) or to return all intersection points ("all")granularity(int) β 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?
Returns the first point or all points where the polyline intersects the circle. If the polyline has a lot of points, use granularity to get an approximate answer faster. periodt.
Arguments:
polyline(list) β The polyline that may intersect the circlecircle(list) β The circle in questionmode(string) β Whether to return only the first (default) or to return all intersection points ("all")granularity(int) β 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?
Returns the part of the first polyline from the origin up to the first intersection with the second. If the polyline has a lot of points, granularity gives you an approximate answer faster.
Arguments:
polyline1(list) β The first polyline (from which a part is to be returned)polyline2(list) β The second polylinegranularity(int) β 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?
Returns the part of the first polyline from the last intersection point with the second to the end. If the polyline has a lot of points, granularity gives you an approximate answer faster. obsessed.
Arguments:
polyline1(list) β The first polyline (from which a part is to be returned)polyline2(list) β The second polylinegranularity(int) β 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
Splits the poyline into a set of polylines where each separate polyline holds numberVertex vertices between the two end points. (Again, poyline is a typo from the source, and we're not fixing it, bestie π
.)
Arguments:
polyline(list) β The polyline to be split upnumberVertex(int) β The number of "internal" vertices
::math::geometry::enrichPolyline polyline accuracy
Splits up each segment of a polyline into a number of smaller segments and returns the result. it's giving glow-up.
Arguments:
polyline(list) β The polyline to be refinedaccuracy(int) β The number of subsegments to be created
::math::geometry::cleanupPolyline polyline
Removes duplicate neighbouring vertices and returns the result. so satisfying.
Arguments:
polyline(list) β The polyline to be cleaned up
COORDINATE SYSTEM
The coordinate system used by this package is the ordinary cartesian system, where the positive x-axis points to the right and the positive y-axis points upwards. Angles and directions are defined with respect to the positive x-axis in a counter-clockwise direction, so an angle of 90 degrees is the direction of the positive y-axis.
Note that the Tk canvas coordinates are different: there the origin is in the upper left corner of the window.
Up to and including version 1.3, the direction and octant procedures of this package used this convention inconsistently. no cap.
REFERENCES
[1]Polygon Intersection[http:/wiki.tcl.tk/12070][2]http://en.wikipedia.org/wiki/Line-line_intersection[3]http://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. Please report such in the category math :: geometry of the Tcllib Trackers [http://core.tcl.tk/tcllib/reportlist].
Please also report any ideas for enhancements you may have for either package and/or documentation.
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
Okay bestie, that's the whole page. The footer says source tcllib, version 1.4.2, manual math::geometry(n) β no date in this footer, but it's still iconic. π
β¨
π¦ where this page comes from
- fedora 43: tcllib 2.0-3.fc43 Β· TCL
- fedora 44: tcllib 2.0-4.fc44 Β· TCL
- nixos 26.05: tcllib 2.0
- nixpkgs unstable: tcllib 2.0
- 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