How to Create Unit Circle and Right Sided Triangle / Pythagorean with Plots pyplot

Hi all,

I want to create a right-sided triangle that also has label for the angle and the side name. The question is how?

I have found this that can plot a triangle :

But still a bit advanced for me to edit and create the label for the angle and the side like hyp, opp, adj.

Next is how to create a unit circle that can have plot on the certain circumference?

Pictures attached.
Thank You

I already find a way to create the right side triangle

here is the code for anyone want to create a right side triangle:

using Plots, LaTeXStrings
pyplot()
import Base: isequal, ==


abstract type GeoObject end
abstract type GeoShape <: GeoObject end

struct Point <: GeoObject
    x::Number
    y::Number
end
(==)(p1::Point, p2::Point) = p1.x==p2.x && p1.y==p2.y

struct Triangle <: GeoShape
    A::Point
    B::Point
    C::Point
end
Triangle(ax, ay, bx, by, cx, cy) = Triangle(Point(ax, ay), Point(bx, by), Point(cx, cy))
(==)(t1::Triangle, t2::Triangle) = vertices(t1) == vertices(t2)

function vertices(tri::Triangle) 
    [tri.A, tri.B, tri.C]
end

struct Edge <: GeoShape
    src::Point
    dst::Point
end

function edges(tri::Triangle)
    elist = Edge[]
    pts = vertices(tri)
    for i in 1:length(pts)-1
        push!(elist, Edge(pts[i], pts[i+1]))
    end
    push!(elist, Edge(pts[length(pts)], pts[1]))
    elist
end

struct Circle
    center::Point
    radius::Number
end
(==)(c1::Circle, c2::Circle) = c1.center == c2.center && c1.radius == c2.radius

ccenter(c::Circle) = c.center

A = Point(2,2); B = Point(6, 2); C = Point(6,5);
tri = Triangle(A, B, C)

function shape(ptlist::Vector{Point})
    xlist = [pt.x for pt in ptlist]
    ylist = [pt.y for pt in ptlist]
    shape = Shape(xlist, ylist)
end;
shape(tri::Triangle) = shape(vertices(tri));

trishape=shape(tri)
    
s1 = L"\theta";
s2 = L"sin \ \theta = \frac{opp}{hyp}";
s3 = L"cos \ \theta = \frac{adj}{hyp}";
s4 = L"tan \ \theta = \frac{opp}{adj}";

plot(trishape, leg=false, fill=(0, :green), aspect_ratio=:equal, fillalpha= 0.2)
scatter!(trishape.x, trishape.y, color=:red)
annotate!([(2.3, 2.1, (s1, 14, :red)), 
            (4, 2.1, ("adj", 10, :red)), 
            (6.2, 3.1, ("opp", 10, :red)),
            (4, 3.7, ("hyp", 10, :red)),
            (2.2, 4.5, (s2, 10, :red)), 
            (2.9, 4.5, (s3, 10, :red)),
            (3.6, 4.5, (s4, 10, :red))])

and for the unit circle:

using Plots
pyplot()

function circleShape(h, k, r)
     θ = LinRange(0, 2*π, 500)
     h .+ r*sin.(θ), k .+ r*cos.(θ)
end

plot(circleShape(0, 0, 1), seriestype = [:shape,], lw = 0.5,
      c =:blue, linecolor = :black,
      legend = false, fillalpha = 0.2, aspect_ratio = 1, showaxis = false)
plot!([0], seriestype="vline", color=:green, label="")
plot!([0], seriestype="hline", color=:green, label="")

annotate!([(1.1,0, ("x", 10, :red)), 
           (0.15,1.03, ("y", 10, :red)), 
           (1.15,-0.1, ("A(1,0)", 10, :red)),
           (0.1,-0.1, ("(0,0)", 10, :red)),
           (0.51,0.95, ("(x,y)", 10, :red))])

scatter!([0.50], [0.866025404], color = "red", label="", markersize = 5)

FWIW, here is a simple right-angled triangle in the same spirit as your circleShape:

using Plots

function RightTriangle(x0, y0, θ, L1, L2)
    s, c = sincosd(θ)
    x1, y1 = L1 .* (c,s) 
    return Shape(x0 .+ [0, x1, x1 - L2*s], y0 .+ [0, y1, y1 + L2*c])
end

RT = RightTriangle(0, 0, 30, 1, 2)
plot(RT, lc=:black, lw=0.5, leg=false, ratio=1, c=:green, fa=0.2)
scatter!(RT, c=:red, ms=2.5)

It works and simpler.

I am changing the length of the sides and the angle to 45 degree

1 Like