Building a Geometric figure with Mathematica.

Not too long ago I wrote here a small piece on creating beautiful figures, I was trying to explain a non trivial example on how to use METAPOST a system created by John D Hobby to build a geometric figure.

I was reading a blog post by Chris Carlson of Wolfram Research named “Always the Right Time for Mathematica” and decided to give Mathematica a try with the same problem in my prior blog post.

So here is the problem again in case you are too lazy to read my prior posting!

Build the following figure. (The lines in red are the altitudes of the triangle)

trianglefig-1.gif

Well, here is the issue. Mathematica is a general purpose computer system but is not specifically design for graphics like METAFONT, still it should be very straight forward to build some figure like above.Fortunately Mathematica is very rich on Mathematics and this should make the solution easy. We can see multiple path of solution to the problem at hand.

Mathematica provide some native commands that allow one to draw lines and point in such and such Cartesian coordinates. So to draw the triangle ABC with Mathematica we can use the following command.

Graphics[{Thick,Blue,

Line[{{0,0},{3,0},{1,2},{0,0}}],

Black,Inset[Text[Style["A",Italic,Large]],{-.1,0}],

Black,Inset[Text[Style["B",Italic,Large]],{3.1,0}],

Black,Inset[Text[Style["C",Italic,Large]],{1.1,2.1}]

}]

]

Where the Line[] Mathematica function draws the triangle and the Inset places the vertex labels in the figure.

triangulefig2-2.gif

The hard part is figuring out how to find the coordinates for the altitudes feet. If we can find a way to get those coordinates then our problem is solve.

Since we are trying to build the altitudes to the triangle. We need to search for properties the altitudes of a triangle satisfy that will facilitate this construction.

Geometrically we can characterize the altitudes of a triangle as the line segment from a vertex to the opposite side and this segment is perpendicular to that side.

triangulefig2-2min.gif

Other property is: the altitude from a vertex is the minimal length segment we can build from the vertex to the opposite side.

And yet another property: One can build by reflection symmetry with respect to the side the vertex when reflected this way will produce another symmetrically opposite vertex and joining those two by a segment , half that segment will be the altitude.

You may find the proof to the above facts in some geometry book. If not you should try to proof them yourself.

We will discuss first the minimal path approach.

It seems that if we can build some function depending on one variable that give us any of the points between B and C then we shall be able to find the distance between any of those points and the point A. We should be able to repeat the same procedure for the other vertex.

triangulefig2-2line.gif

Well it is good to know the linear Bezier curve equation.

B(t)=C+(B-C)t with t from [0,1]

Since B(t) gives us any point between B and C this shall come very handy in our computations.

Now all we need is to find the distance from B(x) to A and then that should give us a function depending on x. Finding the zeros of first derivative to this function we shall be able to find the values of x such that the distance from B(x) to A is minimal.

This can be performed using the following Mathematica code.

m[v_?MatrixQ]:=Module[{x,a=v[[1]],b=v[[2]],c=v[[3]]},

sol=Solve[D[Simplify[EuclideanDistance[c,BAB[x,a,b]],x>0],x]Š0,x];

{Red,Thick,Line[{c,BAB[First[x/.sol],a,b]}]}

]

So all that remains for us is to draw the graph

BAB[t_,a_,b_]=(1-t) a+t b;

m[v_?MatrixQ]:=Module[{x,a=v[[1]],b=v[[2]],c=v[[3]]},

sol=Solve[D[Simplify[EuclideanDistance[c,BAB[x,a,b]],x>0],x]Š0,x];

{Red,Thick,Line[{c,BAB[First[x/.sol],a,b]}]}

]

l[v_?MatrixQ]:=Module[{a=v[[1,1]],col=v[[1,2]],b=v[[2]]},

{col,Inset[Text[Style[a,Italic,Large]],b]}

]

Module[{a={0,0},b={3,0},c={1,2}},

Graphics[{Thick,Blue,

Line[{a,b,c,a}],

Map[m,{{a,b,c},{a,c,b},{b,c,a}}],

Map[l,{{{"A",Red},{-.1,0}},{{"B",Blue},{3.1,0}},{{"C",Black},{1.1,2.1}}}]

}]

]

This solution is valid for A,B and C that form a non obtuse triangle. Can you see why?

I was still not completely satisfied with this solution. Because I wanted something that did not had to use calculus knowledge. So it is fortunate that Mathematica provide other functions like Nearest[], that allow to find from a list of points the closest point to a given point.

I can produce the list of points easily using the table

Something like

Table[BAB[x,{3,0},{1,2}],{x,0,1,0.01}]

And this

Nearest[Table[BAB[x,{3,0},{1,2}],{x,0,1,0.01}],{0,0}]

Should get me a point sufficiently close to the base of the altitude. We repeat the same procedure for the others so the resulting code looks like this.

BAB[t_,a_,b_]=(1-t) a+t b;

Module[{AA=Flatten[Nearest[Table[BAB[x,{3,0},{1,2}],{x,0,1,0.01}],{0,0}]],

BB=Flatten[Nearest[Table[BAB[x,{0,0},{3,0}],{x,0,1,0.01}],{1,2}]],

CC=Flatten[Nearest[Table[BAB[x,{0,0},{1,2}],{x,0,1,0.01}],{3,0}]]

},

Graphics[{Thick,Blue,

Line[{{0,0},{3,0},{1,2},{0,0}}],

Red,Line[{{0,0},AA}],

PointSize[Large],Orange,Point[AA],

Red,Line[{{1,2},BB}],

PointSize[Large],Orange,Point[BB],

Red,Line[{{3,0},CC}],

PointSize[Large],Orange,Point[CC]

}

]

]

Yet one last solution this time using the symmetry of the vertex with respect to each of the side and finding the middle point.

BAB[t_,a_,b_]=(1-t) a+t b;

Module[{rab=ReflectionTransform[Cross[{3,0}-{0,0}],{0,0}],

rac=ReflectionTransform[Cross[{1,2}-{0,0}],{0,0}],

rcb=ReflectionTransform[Cross[{1,2}-{3,0}],{3,0}]

},

Graphics[{Thick,Blue,

Line[{{0,0},{3,0},{1,2},{0,0}}],

Red,Line[{{1,2},BAB[1/2,{1,2},rab[{1,2}]]}],

Red,Line[{{3,0},BAB[1/2,{3,0},rac[{3,0}]]}],

Red,Line[{{0,0},BAB[1/2,{0,0},rcb[{0,0}]]}]

}]

]

triangulefig2-2symm.gif

In this case we are using Cross[] to get the perpendicular vector to a given vector and ReflectionTransform[] will give us the corresponding transformation Matrix that performs the reflection symmetry that we need. This solution should be valid for any triangle define by the points A,B and C.

We can see from the 3 solutions above, that Mathematica is a very powerful system when it comes to mathematical computation and it is not difficult once one have the mathematical knowledge to use a system like Mathematica to come up with a solution to a given problem.

If you do have Mathematica and like to try your hands at this problem then try solving it this other way by finding the intersection of a line perpendicular to the given side that passes by the vertex opposite to the side. This should be fairly straight forward!

This is a blog posting from www.isallaboutmath.com

About these ads

2 Comments

Filed under geometry, graphics, math, Mathematica, mathematics

2 responses to “Building a Geometric figure with Mathematica.

  1. Pingback: Producing animations with Mathematica 6.0 is as easy as pie « Is all about math Weblog

  2. Pingback: One easy problem and One not so easy problem. « Is all about math Weblog

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s