Skip to content

Commit d703298

Browse files
TimelordUKclaude
andcommitted
feat(sql): Add comprehensive vector mathematics support
Implement native vector data type and complete vector math function library for physics calculations, geometry, line analysis, and scientific computations. New Vector DataType: - Added Vector(Vec<f64>) variant to DataValue enum - Display format: "[x,y,z]" - Full integration with JSON, comparisons, serialization, and aggregates - Parse vectors from "[1,2,3]" or "1 2 3" format Vector Functions (10 total): - VEC(x, y, z, ...) - Construct vectors from numeric components - VEC_ADD(v1, v2) - Element-wise vector addition - VEC_SUB(v1, v2) - Element-wise vector subtraction - VEC_SCALE(v, scalar) - Scalar multiplication - VEC_DOT(v1, v2) - Dot product (returns Float) - VEC_MAG(v) - Magnitude/length of vector - VEC_NORMALIZE(v) - Normalize to unit length - VEC_DISTANCE(v1, v2) - Euclidean distance between vectors - VEC_CROSS(v1, v2) - Cross product (3D only) - VEC_ANGLE(v1, v2) - Angle between vectors in radians Real-World Applications: - Physics: velocity, displacement, force calculations - Geometry: line intersection, parallel/perpendicular detection - Point-to-line distance calculations - Angle measurements and direction analysis - Collinearity testing - 3D graphics and transformations Examples added: - examples/vector_math_demo.sql - Basic vector operations - examples/line_intersection_demo.sql - Line geometry analysis - examples/line_relationships_summary.sql - Comprehensive line relationships Usage: SELECT VEC(1, 2, 3) as position; SELECT VEC_ADD(VEC(10,5,0), VEC(5,5,0)) as displacement; SELECT VEC_DOT(VEC(1,2,3), VEC(4,5,6)) as dot_product; SELECT VEC_MAGNITUDE(VEC(3,4)) as distance; SELECT VEC_CROSS(VEC(1,0,0), VEC(0,1,0)) as perpendicular; All vector functions accessible via --list-functions and --function-help. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
1 parent 6a1b156 commit d703298

21 files changed

Lines changed: 1085 additions & 3 deletions
Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
-- Line-Line Intersection Demonstration
2+
-- Shows how to determine if two lines intersect using vector math
3+
4+
-- Example 1: Two intersecting lines in 2D
5+
-- Line 1: from (0,0) to (4,4)
6+
-- Line 2: from (0,4) to (4,0)
7+
SELECT
8+
VEC(0,0) as line1_p1,
9+
VEC(4,4) as line1_p2,
10+
VEC(0,4) as line2_p1,
11+
VEC(4,0) as line2_p2,
12+
13+
-- Direction vectors
14+
VEC_SUB(VEC(4,4), VEC(0,0)) as line1_dir,
15+
VEC_SUB(VEC(4,0), VEC(0,4)) as line2_dir,
16+
17+
-- For 2D lines, use cross product with z=0 to check if parallel
18+
-- If cross product is 0, lines are parallel
19+
VEC_CROSS(
20+
VEC_SUB(VEC(4,4,0), VEC(0,0,0)),
21+
VEC_SUB(VEC(4,0,0), VEC(0,4,0))
22+
) as cross_product,
23+
24+
-- Magnitude of cross product (0 = parallel, non-zero = intersecting/skew)
25+
VEC_MAG(VEC_CROSS(
26+
VEC_SUB(VEC(4,4,0), VEC(0,0,0)),
27+
VEC_SUB(VEC(4,0,0), VEC(0,4,0))
28+
)) as parallel_check;
29+
GO
30+
31+
-- Example 2: Parallel lines (should have cross product magnitude of 0)
32+
-- Line 1: from (0,0) to (4,2)
33+
-- Line 2: from (0,2) to (4,4) - same direction!
34+
SELECT
35+
VEC(0,0) as line1_p1,
36+
VEC(4,2) as line1_p2,
37+
VEC(0,2) as line2_p1,
38+
VEC(4,4) as line2_p2,
39+
40+
VEC_SUB(VEC(4,2), VEC(0,0)) as line1_dir,
41+
VEC_SUB(VEC(4,4), VEC(0,2)) as line2_dir,
42+
43+
-- Check if parallel using 2D cross product (extend to 3D with z=0)
44+
VEC_CROSS(
45+
VEC_SUB(VEC(4,2,0), VEC(0,0,0)),
46+
VEC_SUB(VEC(4,4,0), VEC(0,2,0))
47+
) as cross_product,
48+
49+
VEC_MAG(VEC_CROSS(
50+
VEC_SUB(VEC(4,2,0), VEC(0,0,0)),
51+
VEC_SUB(VEC(4,4,0), VEC(0,2,0))
52+
)) as is_parallel;
53+
GO
54+
55+
-- Example 3: Perpendicular lines (dot product of directions should be 0)
56+
-- Line 1: horizontal (1,0)
57+
-- Line 2: vertical (0,1)
58+
SELECT
59+
VEC(1,0) as line1_dir,
60+
VEC(0,1) as line2_dir,
61+
62+
VEC_DOT(VEC(1,0), VEC(0,1)) as dot_product,
63+
64+
-- Angle should be 90 degrees (π/2 radians)
65+
VEC_ANGLE(VEC(1,0), VEC(0,1)) as angle_rad,
66+
VEC_ANGLE(VEC(1,0), VEC(0,1)) * 180 / PI() as angle_deg;
67+
GO
68+
69+
-- Example 4: 3D line intersection check
70+
-- Two lines in 3D space
71+
SELECT
72+
VEC(0,0,0) as line1_p1,
73+
VEC(1,1,1) as line1_p2,
74+
VEC(0,1,0) as line2_p1,
75+
VEC(1,0,0) as line2_p2,
76+
77+
-- Direction vectors
78+
VEC_SUB(VEC(1,1,1), VEC(0,0,0)) as line1_dir,
79+
VEC_SUB(VEC(1,0,0), VEC(0,1,0)) as line2_dir,
80+
81+
-- Cross product (perpendicular to both)
82+
VEC_CROSS(
83+
VEC_SUB(VEC(1,1,1), VEC(0,0,0)),
84+
VEC_SUB(VEC(1,0,0), VEC(0,1,0))
85+
) as perpendicular_vec,
86+
87+
-- If magnitude is 0, lines are parallel
88+
VEC_MAG(VEC_CROSS(
89+
VEC_SUB(VEC(1,1,1), VEC(0,0,0)),
90+
VEC_SUB(VEC(1,0,0), VEC(0,1,0))
91+
)) as parallel_check;
92+
GO
93+
94+
-- Example 5: Angle between two line segments
95+
-- Useful for detecting sharp vs smooth intersections
96+
SELECT
97+
VEC(1,0) as dir1,
98+
VEC(1,1) as dir2,
99+
100+
-- Angle between lines
101+
VEC_ANGLE(VEC(1,0), VEC(1,1)) as angle_rad,
102+
VEC_ANGLE(VEC(1,0), VEC(1,1)) * 180 / PI() as angle_deg,
103+
104+
-- Normalize first to ensure we're comparing directions
105+
VEC_ANGLE(
106+
VEC_NORMALIZE(VEC(1,0)),
107+
VEC_NORMALIZE(VEC(1,1))
108+
) * 180 / PI() as normalized_angle;
109+
GO
110+
111+
-- Example 6: Distance from point to line
112+
-- Using the formula: distance = ||(p - a) × dir|| / ||dir||
113+
-- where p is the point, a is a point on the line, dir is line direction
114+
SELECT
115+
VEC(2,2) as point,
116+
VEC(0,0) as line_point,
117+
VEC(1,0) as line_dir,
118+
119+
-- Vector from line point to target point
120+
VEC_SUB(VEC(2,2), VEC(0,0)) as to_point,
121+
122+
-- Cross product (needs to be 3D)
123+
VEC_CROSS(
124+
VEC_SUB(VEC(2,2,0), VEC(0,0,0)),
125+
VEC(1,0,0)
126+
) as cross_vec,
127+
128+
-- Distance = magnitude of cross / magnitude of direction
129+
VEC_MAG(VEC_CROSS(
130+
VEC_SUB(VEC(2,2,0), VEC(0,0,0)),
131+
VEC(1,0,0)
132+
)) / VEC_MAG(VEC(1,0,0)) as distance_to_line;
133+
GO
Lines changed: 184 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,184 @@
1+
-- LINE RELATIONSHIPS: What We Can Determine
2+
-- Using vector math to analyze relationships between lines
3+
4+
-- =============================================================================
5+
-- 1. PARALLEL LINES CHECK
6+
-- =============================================================================
7+
-- Two lines are parallel if their direction vectors are parallel
8+
-- Test: Cross product magnitude = 0
9+
10+
SELECT '=== PARALLEL LINES ===' as test;
11+
GO
12+
13+
-- Parallel lines (same direction)
14+
SELECT
15+
'Parallel' as relationship,
16+
VEC(0,0) as line1_start,
17+
VEC(2,2) as line1_end,
18+
VEC(1,1) as line2_start,
19+
VEC(3,3) as line2_end,
20+
21+
VEC_MAG(VEC_CROSS(
22+
VEC(2,2,0), -- line1 direction
23+
VEC(2,2,0) -- line2 direction (same!)
24+
)) as cross_mag,
25+
26+
CASE
27+
WHEN VEC_MAG(VEC_CROSS(VEC(2,2,0), VEC(2,2,0))) < 0.0001
28+
THEN 'PARALLEL'
29+
ELSE 'NOT PARALLEL'
30+
END as result;
31+
GO
32+
33+
-- =============================================================================
34+
-- 2. INTERSECTING LINES CHECK
35+
-- =============================================================================
36+
-- Two lines intersect if they're NOT parallel (cross product ≠ 0)
37+
38+
SELECT '=== INTERSECTING LINES ===' as test;
39+
GO
40+
41+
-- Classic X intersection
42+
SELECT
43+
'Intersecting' as relationship,
44+
VEC(0,0) as line1_start,
45+
VEC(4,4) as line1_end,
46+
VEC(0,4) as line2_start,
47+
VEC(4,0) as line2_end,
48+
49+
VEC_MAG(VEC_CROSS(
50+
VEC(4,4,0), -- line1 direction
51+
VEC(4,-4,0) -- line2 direction
52+
)) as cross_mag,
53+
54+
CASE
55+
WHEN VEC_MAG(VEC_CROSS(VEC(4,4,0), VEC(4,-4,0))) > 0.0001
56+
THEN 'INTERSECT'
57+
ELSE 'PARALLEL'
58+
END as result,
59+
60+
-- Expected intersection point is (2,2) for this case
61+
'Expected: (2,2)' as note;
62+
GO
63+
64+
-- =============================================================================
65+
-- 3. PERPENDICULAR LINES CHECK
66+
-- =============================================================================
67+
-- Two lines are perpendicular if dot product of directions = 0
68+
69+
SELECT '=== PERPENDICULAR LINES ===' as test;
70+
GO
71+
72+
SELECT
73+
'Perpendicular' as relationship,
74+
VEC(1,0) as line1_dir,
75+
VEC(0,1) as line2_dir,
76+
77+
VEC_DOT(VEC(1,0), VEC(0,1)) as dot_product,
78+
79+
CASE
80+
WHEN ABS(VEC_DOT(VEC(1,0), VEC(0,1))) < 0.0001
81+
THEN 'PERPENDICULAR'
82+
ELSE 'NOT PERPENDICULAR'
83+
END as result,
84+
85+
VEC_ANGLE(VEC(1,0), VEC(0,1)) * 180 / PI() as angle_degrees;
86+
GO
87+
88+
-- =============================================================================
89+
-- 4. ANGLE BETWEEN LINES
90+
-- =============================================================================
91+
-- Can determine the angle of intersection
92+
93+
SELECT '=== ANGLE BETWEEN LINES ===' as test;
94+
GO
95+
96+
SELECT
97+
'Acute angle' as case_type,
98+
VEC(1,0) as line1_dir,
99+
VEC(1,1) as line2_dir,
100+
VEC_ANGLE(VEC(1,0), VEC(1,1)) * 180 / PI() as angle_deg;
101+
GO
102+
103+
SELECT
104+
'Right angle' as case_type,
105+
VEC(1,0) as line1_dir,
106+
VEC(0,1) as line2_dir,
107+
VEC_ANGLE(VEC(1,0), VEC(0,1)) * 180 / PI() as angle_deg;
108+
GO
109+
110+
SELECT
111+
'Obtuse angle' as case_type,
112+
VEC(1,0) as line1_dir,
113+
VEC(-1,1) as line2_dir,
114+
VEC_ANGLE(VEC(1,0), VEC(-1,1)) * 180 / PI() as angle_deg;
115+
GO
116+
117+
-- =============================================================================
118+
-- 5. DISTANCE FROM POINT TO LINE
119+
-- =============================================================================
120+
-- Can compute perpendicular distance from any point to a line
121+
122+
SELECT '=== POINT-TO-LINE DISTANCE ===' as test;
123+
GO
124+
125+
SELECT
126+
VEC(2,2) as point,
127+
'y=0 (x-axis)' as line,
128+
VEC(0,0) as line_point,
129+
VEC(1,0) as line_dir,
130+
131+
VEC_MAG(VEC_CROSS(
132+
VEC(2,2,0),
133+
VEC(1,0,0)
134+
)) / VEC_MAG(VEC(1,0,0)) as distance,
135+
136+
'Expected: 2' as verification;
137+
GO
138+
139+
-- =============================================================================
140+
-- 6. COLLINEAR CHECK
141+
-- =============================================================================
142+
-- Three points are collinear if cross product of vectors between them = 0
143+
144+
SELECT '=== COLLINEAR POINTS ===' as test;
145+
GO
146+
147+
SELECT
148+
'Collinear' as case_type,
149+
VEC(0,0) as p1,
150+
VEC(1,1) as p2,
151+
VEC(2,2) as p3,
152+
153+
VEC_MAG(VEC_CROSS(
154+
VEC_SUB(VEC(1,1,0), VEC(0,0,0)),
155+
VEC_SUB(VEC(2,2,0), VEC(0,0,0))
156+
)) as cross_mag,
157+
158+
CASE
159+
WHEN VEC_MAG(VEC_CROSS(
160+
VEC_SUB(VEC(1,1,0), VEC(0,0,0)),
161+
VEC_SUB(VEC(2,2,0), VEC(0,0,0))
162+
)) < 0.0001
163+
THEN 'COLLINEAR'
164+
ELSE 'NOT COLLINEAR'
165+
END as result;
166+
GO
167+
168+
-- =============================================================================
169+
-- SUMMARY: What we CAN determine
170+
-- =============================================================================
171+
-- ✓ Are lines parallel?
172+
-- ✓ Are lines perpendicular?
173+
-- ✓ Do lines intersect (in 2D/3D)?
174+
-- ✓ What angle do lines make?
175+
-- ✓ Distance from point to line
176+
-- ✓ Are three points collinear?
177+
-- ✓ Direction of lines
178+
-- ✓ Relative orientation (cross product sign)
179+
180+
-- What would make this even more powerful:
181+
-- • Computing exact intersection point (needs parametric solving)
182+
-- • Closest point on line to another point
183+
-- • Line-segment intersection (requires bounds checking)
184+
-- • 3D skew lines distance

0 commit comments

Comments
 (0)