Skip to content

Commit f7fa74f

Browse files
committedFeb 24, 2023
v.1.0.0 contd
* line.parallelTo, line.perpendicularTo * some more utils for 3D * update manual
1 parent 6481be9 commit f7fa74f

File tree

6 files changed

+231
-8
lines changed

6 files changed

+231
-8
lines changed
 

‎build/Geometrize.js

+87-4
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@
22
* Geometrize
33
* computational geometry and rendering library for JavaScript
44
*
5-
* @version 1.0.0 (2023-02-23 11:55:45)
5+
* @version 1.0.0 (2023-02-24 15:46:51)
66
* https://github.com/foo123/Geometrize
77
*
88
**//**
99
* Geometrize
1010
* computational geometry and rendering library for JavaScript
1111
*
12-
* @version 1.0.0 (2023-02-23 11:55:45)
12+
* @version 1.0.0 (2023-02-24 15:46:51)
1313
* https://github.com/foo123/Geometrize
1414
*
1515
**/
@@ -915,6 +915,10 @@ var Point2D = makeClass(Object2D, {
915915
self.$super('dispose');
916916
};
917917
},
918+
/**[DOC_MD]
919+
* **Methods:**
920+
*
921+
[/DOC_MD]**/
918922
name: 'Point2D',
919923
clone: function() {
920924
return new Point2D(this.x, this.y);
@@ -931,6 +935,9 @@ var Point2D = makeClass(Object2D, {
931935
xmax: self.x
932936
};
933937
},
938+
/**[DOC_MD]
939+
* * `eq(point: Point2D|Object{x,y}|[x,y]): Bool` determine if equals another point-like
940+
[/DOC_MD]**/
934941
eq: function(other) {
935942
var self = this;
936943
if (other instanceof Point2D)
@@ -947,26 +954,48 @@ var Point2D = makeClass(Object2D, {
947954
}
948955
return false;
949956
},
957+
/**[DOC_MD]
958+
* * `add(other: Point2D): Point2D` add points coordinate-wise
959+
* * `add(other: Number): Point2D` add number to point coordinates
960+
[/DOC_MD]**/
950961
add: function(other) {
951962
var self = this;
952963
return other instanceof Point2D ? new Point2D(self.x+other.x, self.y+other.y) : new Point2D(self.x+Num(other), self.y+Num(other));
953964
},
965+
/**[DOC_MD]
966+
* * `mul(other: Number): Point2D` multiply number to point coordinates
967+
[/DOC_MD]**/
954968
mul: function(other) {
955969
other = Num(other);
956970
return new Point2D(this.x*other, this.y*other);
957971
},
972+
/**[DOC_MD]
973+
* * `dot(other: Point2D): Number` dot product of points
974+
[/DOC_MD]**/
958975
dot: function(other) {
959976
return dotp(this.x, this.y, other.x, other.y);
960977
},
978+
/**[DOC_MD]
979+
* * `cross(other: Point2D): Number` cross product of points
980+
[/DOC_MD]**/
961981
cross: function(other) {
962982
return crossp(this.x, this.y, other.x, other.y);
963983
},
984+
/**[DOC_MD]
985+
* * `angle(other: Point2D): Number` angle between points
986+
[/DOC_MD]**/
964987
angle: function(other) {
965988
return angle(this.x, this.y, other.x, other.y);
966989
},
990+
/**[DOC_MD]
991+
* * `between(p1: Point2D, p1: Point2D): Bool` check if point is on line segment defined by points p1,p2
992+
[/DOC_MD]**/
967993
between: function(p1, p2) {
968994
return point_on_line_segment(this, p1, p2);
969995
},
996+
/**[DOC_MD]
997+
* * `distanceToLine(p1: Point2D, p1: Point2D): Number` distance of point to line defined by points p1,p2
998+
[/DOC_MD]**/
970999
distanceToLine: function(p1, p2) {
9711000
return point_line_distance(this, p1, p2);
9721001
},
@@ -2368,6 +2397,10 @@ var Line = makeClass(Bezier2D, {
23682397
return self.$super('isChanged', arguments);
23692398
};
23702399
},
2400+
/**[DOC_MD]
2401+
* **Methods:**
2402+
*
2403+
[/DOC_MD]**/
23712404
name: 'Line',
23722405
clone: function() {
23732406
var self = this;
@@ -2434,8 +2467,27 @@ var Line = makeClass(Bezier2D, {
24342467
}
24352468
return false;
24362469
},
2437-
distanceToPoint: function(point) {
2438-
return point_line_segment_distance(point, this._points[0], this._points[1]);
2470+
/**[DOC_MD]
2471+
* * `distanceToPoint(p: Point2D): Number` distance of point to this line segment
2472+
[/DOC_MD]**/
2473+
distanceToPoint: function(p) {
2474+
return point_line_segment_distance(p, this._points[0], this._points[1]);
2475+
},
2476+
/**[DOC_MD]
2477+
* * `isParallelTo(l: Line): Bool` determine if line is parallel to line l
2478+
* * `isParallelTo(p: Point2D, q: Point2D): Bool` determine if line is parallel to line defined by points p,q
2479+
[/DOC_MD]**/
2480+
isParallelTo: function(p, q) {
2481+
var _p = this._points;
2482+
return p instanceof Line ? lines_parallel(_p[0], _p[1], p._points[0], p._points[1]) : lines_parallel(_p[0], _p[1], p, q);
2483+
},
2484+
/**[DOC_MD]
2485+
* * `isPerpendicularTo(l: Line): Bool` determine if line is perpendicular to line l
2486+
* * `isPerpendicularTo(p: Point2D, q: Point2D): Bool` determine if line is perpendicular to line defined by points p,q
2487+
[/DOC_MD]**/
2488+
isPerpendicularTo: function(p, q) {
2489+
var _p = this._points;
2490+
return p instanceof Line ? lines_perpendicular(_p[0], _p[1], p._points[0], p._points[1]) : lines_perpendicular(_p[0], _p[1], p, q);
24392491
},
24402492
bezierPoints: function(t) {
24412493
if (arguments.length) t = clamp(t, 0, 1);
@@ -4949,6 +5001,14 @@ function point_line_segment_distance(p0, p1, p2)
49495001
t = stdMath.max(0, stdMath.min(1, ((x - x1)*dx + (y - y1)*dy) / d));
49505002
return hypot(x - x1 - t*dx, y - y1 - t*dy);
49515003
}
5004+
function lines_parallel(p1, p2, q1, q2)
5005+
{
5006+
return is_almost_equal((p2.y - p1.y)*(q2.x - q1.x), (q2.y - q1.y)*(p2.x - p1.x));
5007+
}
5008+
function lines_perpendicular(p1, p2, q1, q2)
5009+
{
5010+
return is_almost_equal((p2.y - p1.y)*(q2.y - q1.y), -(p2.x - p1.x)*(q2.x - q1.x));
5011+
}
49525012
function point_on_line_segment(p, p1, p2)
49535013
{
49545014
var t = 0,
@@ -5478,6 +5538,29 @@ function polyline_area(polyline_points)
54785538
}
54795539
return area;
54805540
}
5541+
function plane_plane_intersection(a, b, c, d, k, l, m, n)
5542+
{
5543+
var D = a*l - b*k;
5544+
// none, line or single point
5545+
if (is_strictly_equal(D, 0)) return false; // none
5546+
// one or two points (a line)
5547+
// x:(b*(m*t + n) - l*(c*t+d))/D, y:(k*(c*t+d) - a*(m*t + n))/D, z:t
5548+
return [
5549+
{x:(b*n - l*d)/D, y:(k*d - a*n)/D, z:0},
5550+
{x:(b*(m+n) - l*(c+d))/D, y:(k*(c+d) - a*(m+n))/D, z:1}
5551+
];
5552+
}
5553+
function normal_to_plane(a, b, c, d)
5554+
{
5555+
return {x:a, y:b, z:c};
5556+
}
5557+
function normal_to_points(p1, p2, p3)
5558+
{
5559+
return crossp3(
5560+
p2.x - p1.x, p2.y - p1.y, p2.z - p1.z,
5561+
p3.x - p1.x, p3.y - p1.y, p3.z - p1.z
5562+
);
5563+
}
54815564
function convex_hull(points)
54825565
{
54835566
var pc = points.length, p0, i0, i, p, ps, convexHull, hullSize;

‎build/Geometrize.min.js

+2-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎manual.md

+57
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,44 @@ p.y = 5; // change it
139139

140140

141141

142+
**Methods:**
143+
144+
145+
146+
147+
* `eq(point: Point2D|Object{x,y}|[x,y]): Bool` determine if equals another point-like
148+
149+
150+
151+
* `add(other: Point2D): Point2D` add points coordinate-wise
152+
* `add(other: Number): Point2D` add number to point coordinates
153+
154+
155+
156+
* `mul(other: Number): Point2D` multiply number to point coordinates
157+
158+
159+
160+
* `dot(other: Point2D): Number` dot product of points
161+
162+
163+
164+
* `cross(other: Point2D): Number` cross product of points
165+
166+
167+
168+
* `angle(other: Point2D): Number` angle between points
169+
170+
171+
172+
* `between(p1: Point2D, p1: Point2D): Bool` check if point is on line segment defined by points p1,p2
173+
174+
175+
176+
* `distanceToLine(p1: Point2D, p1: Point2D): Number` distance of point to line defined by points p1,p2
177+
178+
179+
142180
### Topos2D (subclass of Object2D)
143181

144182
Represents a geometric topos, ie a set of points
@@ -271,6 +309,25 @@ line.end.y = 20; // change it
271309

272310

273311

312+
**Methods:**
313+
314+
315+
316+
317+
* `distanceToPoint(p: Point2D): Number` distance of point to this line segment
318+
319+
320+
321+
* `isParallelTo(l: Line): Bool` determine if line is parallel to line l
322+
* `isParallelTo(p: Point2D, q: Point2D): Bool` determine if line is parallel to line defined by points p,q
323+
324+
325+
326+
* `isPerpendicularTo(l: Line): Bool` determine if line is perpendicular to line l
327+
* `isPerpendicularTo(p: Point2D, q: Point2D): Bool` determine if line is perpendicular to line defined by points p,q
328+
329+
330+
274331
### QBezier (subclass of Bezier2D)
275332

276333
Represents a quadratic Bezier curve defined by its control points

‎src/Line.js

+25-2
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,10 @@ var Line = makeClass(Bezier2D, {
8888
return self.$super('isChanged', arguments);
8989
};
9090
},
91+
/**[DOC_MD]
92+
* **Methods:**
93+
*
94+
[/DOC_MD]**/
9195
name: 'Line',
9296
clone: function() {
9397
var self = this;
@@ -154,8 +158,27 @@ var Line = makeClass(Bezier2D, {
154158
}
155159
return false;
156160
},
157-
distanceToPoint: function(point) {
158-
return point_line_segment_distance(point, this._points[0], this._points[1]);
161+
/**[DOC_MD]
162+
* * `distanceToPoint(p: Point2D): Number` distance of point to this line segment
163+
[/DOC_MD]**/
164+
distanceToPoint: function(p) {
165+
return point_line_segment_distance(p, this._points[0], this._points[1]);
166+
},
167+
/**[DOC_MD]
168+
* * `isParallelTo(l: Line): Bool` determine if line is parallel to line l
169+
* * `isParallelTo(p: Point2D, q: Point2D): Bool` determine if line is parallel to line defined by points p,q
170+
[/DOC_MD]**/
171+
isParallelTo: function(p, q) {
172+
var _p = this._points;
173+
return p instanceof Line ? lines_parallel(_p[0], _p[1], p._points[0], p._points[1]) : lines_parallel(_p[0], _p[1], p, q);
174+
},
175+
/**[DOC_MD]
176+
* * `isPerpendicularTo(l: Line): Bool` determine if line is perpendicular to line l
177+
* * `isPerpendicularTo(p: Point2D, q: Point2D): Bool` determine if line is perpendicular to line defined by points p,q
178+
[/DOC_MD]**/
179+
isPerpendicularTo: function(p, q) {
180+
var _p = this._points;
181+
return p instanceof Line ? lines_perpendicular(_p[0], _p[1], p._points[0], p._points[1]) : lines_perpendicular(_p[0], _p[1], p, q);
159182
},
160183
bezierPoints: function(t) {
161184
if (arguments.length) t = clamp(t, 0, 1);

‎src/Point2D.js

+29
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,10 @@ var Point2D = makeClass(Object2D, {
9494
self.$super('dispose');
9595
};
9696
},
97+
/**[DOC_MD]
98+
* **Methods:**
99+
*
100+
[/DOC_MD]**/
97101
name: 'Point2D',
98102
clone: function() {
99103
return new Point2D(this.x, this.y);
@@ -110,6 +114,9 @@ var Point2D = makeClass(Object2D, {
110114
xmax: self.x
111115
};
112116
},
117+
/**[DOC_MD]
118+
* * `eq(point: Point2D|Object{x,y}|[x,y]): Bool` determine if equals another point-like
119+
[/DOC_MD]**/
113120
eq: function(other) {
114121
var self = this;
115122
if (other instanceof Point2D)
@@ -126,26 +133,48 @@ var Point2D = makeClass(Object2D, {
126133
}
127134
return false;
128135
},
136+
/**[DOC_MD]
137+
* * `add(other: Point2D): Point2D` add points coordinate-wise
138+
* * `add(other: Number): Point2D` add number to point coordinates
139+
[/DOC_MD]**/
129140
add: function(other) {
130141
var self = this;
131142
return other instanceof Point2D ? new Point2D(self.x+other.x, self.y+other.y) : new Point2D(self.x+Num(other), self.y+Num(other));
132143
},
144+
/**[DOC_MD]
145+
* * `mul(other: Number): Point2D` multiply number to point coordinates
146+
[/DOC_MD]**/
133147
mul: function(other) {
134148
other = Num(other);
135149
return new Point2D(this.x*other, this.y*other);
136150
},
151+
/**[DOC_MD]
152+
* * `dot(other: Point2D): Number` dot product of points
153+
[/DOC_MD]**/
137154
dot: function(other) {
138155
return dotp(this.x, this.y, other.x, other.y);
139156
},
157+
/**[DOC_MD]
158+
* * `cross(other: Point2D): Number` cross product of points
159+
[/DOC_MD]**/
140160
cross: function(other) {
141161
return crossp(this.x, this.y, other.x, other.y);
142162
},
163+
/**[DOC_MD]
164+
* * `angle(other: Point2D): Number` angle between points
165+
[/DOC_MD]**/
143166
angle: function(other) {
144167
return angle(this.x, this.y, other.x, other.y);
145168
},
169+
/**[DOC_MD]
170+
* * `between(p1: Point2D, p1: Point2D): Bool` check if point is on line segment defined by points p1,p2
171+
[/DOC_MD]**/
146172
between: function(p1, p2) {
147173
return point_on_line_segment(this, p1, p2);
148174
},
175+
/**[DOC_MD]
176+
* * `distanceToLine(p1: Point2D, p1: Point2D): Number` distance of point to line defined by points p1,p2
177+
[/DOC_MD]**/
149178
distanceToLine: function(p1, p2) {
150179
return point_line_distance(this, p1, p2);
151180
},

‎src/utils.js

+31
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,14 @@ function point_line_segment_distance(p0, p1, p2)
3131
t = stdMath.max(0, stdMath.min(1, ((x - x1)*dx + (y - y1)*dy) / d));
3232
return hypot(x - x1 - t*dx, y - y1 - t*dy);
3333
}
34+
function lines_parallel(p1, p2, q1, q2)
35+
{
36+
return is_almost_equal((p2.y - p1.y)*(q2.x - q1.x), (q2.y - q1.y)*(p2.x - p1.x));
37+
}
38+
function lines_perpendicular(p1, p2, q1, q2)
39+
{
40+
return is_almost_equal((p2.y - p1.y)*(q2.y - q1.y), -(p2.x - p1.x)*(q2.x - q1.x));
41+
}
3442
function point_on_line_segment(p, p1, p2)
3543
{
3644
var t = 0,
@@ -560,6 +568,29 @@ function polyline_area(polyline_points)
560568
}
561569
return area;
562570
}
571+
function plane_plane_intersection(a, b, c, d, k, l, m, n)
572+
{
573+
var D = a*l - b*k;
574+
// none, line or single point
575+
if (is_strictly_equal(D, 0)) return false; // none
576+
// one or two points (a line)
577+
// x:(b*(m*t + n) - l*(c*t+d))/D, y:(k*(c*t+d) - a*(m*t + n))/D, z:t
578+
return [
579+
{x:(b*n - l*d)/D, y:(k*d - a*n)/D, z:0},
580+
{x:(b*(m+n) - l*(c+d))/D, y:(k*(c+d) - a*(m+n))/D, z:1}
581+
];
582+
}
583+
function normal_to_plane(a, b, c, d)
584+
{
585+
return {x:a, y:b, z:c};
586+
}
587+
function normal_to_points(p1, p2, p3)
588+
{
589+
return crossp3(
590+
p2.x - p1.x, p2.y - p1.y, p2.z - p1.z,
591+
p3.x - p1.x, p3.y - p1.y, p3.z - p1.z
592+
);
593+
}
563594
function convex_hull(points)
564595
{
565596
var pc = points.length, p0, i0, i, p, ps, convexHull, hullSize;

0 commit comments

Comments
 (0)
Please sign in to comment.