Skip to content

Instantly share code, notes, and snippets.

@XerxesZorgon
Created September 28, 2024 01:21
Show Gist options
  • Select an option

  • Save XerxesZorgon/ac2455008735d62b5d5214224a1e7f20 to your computer and use it in GitHub Desktop.

Select an option

Save XerxesZorgon/ac2455008735d62b5d5214224a1e7f20 to your computer and use it in GitHub Desktop.
Solves a geometry problem using linear algebra and Heron's formula
#=
Solves a geometry problem using linear algebra and Heron's formula
See: Red Flag Day Geometry
using Revise
Load with: includet("redflag.jl")
=#
using LinearAlgebra
"""
p_int = line_intersect(p,q,r,s)
Point of intersection of two lines
Paramterrs:
p: Point on line 1, 2x1 Vector
q: Vector direction of line 1, 2x1 Vector
r: Point on line 2, 2x1 Vector
s: Vector direction of line 2, 2x1 Vector
Returns:
p_int: Point of intersection of the two lines
"""
function line_intersect(p::Vector{},q::Vector{},r::Vector{},s::Vector{})
# Check that all inputs are length 3
if length(p) == 2
push!(p,0)
end
if length(q) == 2
push!(q,0)
end
if length(r) == 2
push!(r,0)
end
if length(s) == 2
push!(s,0)
end
# Bezier parameter for point of intersection
u = norm(cross(r-p,s))/norm(cross(q,s))
# Point of intersection
p_int = p + u * q
# Remove zero from third dimension
pop!(p)
pop!(q)
pop!(r)
pop!(s)
pop!(p_int)
# Return the vector
return p_int
end
"""
A = herons(v1,v2,v3)
Returns the area of a triangle using Heron's formula
Paramterrs:
v1,v2,v3: Vertices of the triangle, 2x1
Returns:
A: Area of the triangle
"""
function herons(v1::Vector{}, v2::Vector{}, v3::Vector{})
# Lengths of each side
a = norm(v2 - v1)
b = norm(v3 - v2)
c = norm(v1 - v3)
# Semiperimter
s = (a + b + c)/2
# Triangle area
A = sqrt(s*(s-a)*(s-b)*(s-c))
return A
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment