# Projection of vector

I'm working to reproduce an example made in GeoGebra* using Sage. What I got in GeoGebra looks like this, The goal is to find the length of _j_ only knowing _A_, _B_, and _C_.

Here's where I'm at in Sage, it seems really smart, but all help files and templates I find online feels way too advanced for where I'm currently at. I've written the code below and it's currently look like this, code (thanks to @tmonteil I've been able to update my code with some calculations, I am still struggling to produce the plot),

A = (9, 5); B = (2, 4); C = (16, -2);
AB = vector(B)-vector(A)
BC = vector(C)-vector(B)
P = plot(x,(x,2,5), color='red')
P += AB.plot(color='green', start=A)
P += BC.plot(color='green', start=B)
print "Length of AB proj. onto BC"
show(P, figsize=5, aspect_ratio=1)

# show(AB.inner_product(BC)/BC.norm()^2*v2)
show(AB.inner_product(BC)/BC.norm())
RDF(AB.inner_product(BC)/BC.norm())

edit retag close merge delete

Sort by » oldest newest most voted

Your approach will not work since you try to play with plots. Instead, you have to play with vectors, not their plots.

To my understanding of the picture, D is the orthogonal projection of A to the line (B,C).

If v1 denotes the vector BA and v2 denotes the vector BC, the length j is the inner product of v1 by v2 divided by the norm of v2. Here is how to do it in Sage:

sage: A = (9, 5); B = (2, 4); C = (16, -2);
sage: v1 = vector(A)-vector(B)
sage: v2 = vector(C)-vector(B)
sage: v1.inner_product(v2)/v2.norm()
23/29*sqrt(58)


If you want a floating approximation, you can do:

sage: RDF(v1.inner_product(v2)/v2.norm())
6.040095911547238


EDIT : When you write

sage: plot(x,(x,2,5),color='red')


You ask Sage to plot the function x (i.e. the identity function), for x varying from 2 to 5.

To compute the coordinates of D, you can do:

sage: BA = -AB
sage: D = vector(B) + BC*BA.inner_product(BC)/BC.norm()/BC.norm()
sage: D
(219/29, 47/29)


To add the point D on the picture, you can do:

sage: P += point(D)


If you want the x-axis and the y-axis to have the same scale (so that the right angles are visible), you can do:

sage: P.set_aspect_ratio(1)

more

2

@ndomes, thanks! That is amazing! If you add it as an answer I'll be happy to mark it answering my question!

@ndomes, how did you create this page http://sagecell.sagemath.org/?q=dgweis do you have an account somewhere or? I am interested to share some sage code online.