Graphics Computations

Applied to MATh.en.JEANS research workshop themes

January 17, 2025 - Yves Papegay - yap@informathiques.fr

This notebook is the final version (after session) of the support of the third session of the course “Online teacher training and community of practices - Digital tools for STEAM education and Makers pedagogy, November 2024 - June 2025” , an activity of the “MEJ MAKERS ERASMUS+ project (2023-2-FR01-KA210-SCH-000176068)” - visit the course dedicated web page for details.

Tensed cables

Drawing point, lines and polygons

Points

Draw a point, and control its size.

Graphics[{PointSize[0.2],Point[{0,0}]},Frame->True]

Draw a list of point of different colors.

Graphics[{PointSize[0.2],Point[{0,0}],Red,Point[{1,1}],Blue,Point[{2,2}]},Frame->True]

If it is not necessary to change the color (or the size), Point can handle a list of points.

Graphics[{PointSize[0.2],Point[{{0,0},{1,1},{2,1},{2,2}}]},Frame->True]

Lines

Draw a line, and control its thickness.

Graphics[{Thickness[0.05],Line[{{0,0},{1,1},{2,1},{2,2},{0,1},{0,0}}]},Frame->True]

Polygons

Draw a polygon, non necessarily convex.

Graphics[{Pink,Polygon[{{0,0},{1,1},{2,1},{2,2},{0,1}}]},Frame->True]

Draw a triangle from its vertices.

Graphics[{Pink,Triangle[{{0,0},{2,0},{1,2}}]},Frame->True]

Draw a triangle from its lengths of edges.

Graphics[{Pink,SSSTriangle[Sqrt[5],Sqrt[5],2]},Frame->True]

Draw a triangle from two angles and the length of one edge.

Graphics[{Pink,ASATriangle[ArcTan[2],2,ArcTan[2]]},Frame->True]

Draw two rectangles of different colors.

Graphics[{Red,Rectangle[{0,0},{1,3}],Blue,Rectangle[{2,1},{4,2}]},Frame->True]

Draw a parallelogram.

Graphics[{Pink,Parallelogram[{0,0},{{1,0},{1,2}}]},Frame->True]

Draw a random convex polygon with a given number of edges.

Graphics[{Pink,RandomPolygon[{"Convex",5}]},Frame->True]

Draw interactively regular polygons with different number of edges.

Graphics[{Pink,RegularPolygon[5]}]

Manipulate[Graphics[{Circle[],Pink,RegularPolygon[n]}],{n,3,20,1}]

Constructing a first example

Like on the figure at the top of the document, we consider here a square with 8 points on each edge and the same square rotated by an angle of 58 degrees.

simple polygon

Build the list of points.

Table[{i,0},{i,0,7}]

Table[{7,i},{i,1,7}]

P=Join[Table[{i,0},{i,1,7}],Table[{7,i},{i,1,7}],Table[{i-1,7},{i,7,1,-1}],Table[{0,i-1},{i,7,1,-1}]]

Draw it (in a nice way).

Graphics[{{Gray,Dashed,Line[{P[[28]],P[[7]],P[[14]],P[[21]],P[[28]]}]},PointSize[0.03],Point[P]}]

transformed polygon

Define the center of the figure (which is also the center of the transformation)

G=RegionCentroid[Polygon[P]]

Define the transformation (on an example of point)

RotationMatrix[α]//MatrixForm

RotationMatrix[α].(P[[1]]-G)+G

Check the result on one point.

Q1=(RotationMatrix[α].(P[[1]]-G)+G)/.α->58Degree

Draw it.

Graphics[{{Gray,Dashed,Line[{P[[28]],P[[7]],P[[14]],P[[21]],P[[28]]}],Line[{{P[[1]],G}}],Line[{G,Q1}]},PointSize[0.03],Point[P],{Red,Point[G]},{Pink,Point[Q1]}}]

Define the transformation (as a function of a point, an angle, and a center)

transform[p_,a_,c_]:=RotationMatrix[a].(p-c)+c

transform[P[[1]],58 Degree,G]

Q1

Compute the list of transformed points.

Map[f,{l1,l2,l3}]

(transform[#,58Degree,G]&)[P[[1]]]

Q=Map[transform[#,58Degree,G]&,P];

Draw them.

Graphics[{{Gray,Dashed,Line[{P[[28]],P[[7]],P[[14]],P[[21]],P[[28]]}],Pink,Line[{Q[[28]],Q[[7]],Q[[14]],Q[[21]],Q[[28]]}]},Table[Line[{P[[i]],Q[[i]]}],{i,1,28}],PointSize[0.03],Point[P],Pink,Point[Q]}]

Making the example more generic and interactive

Combining the computations

Use the With construct to reduce visibility on variable and combine the computations together.

With[{P=Join[Table[{i,0},{i,1,7}],Table[{7,i},{i,1,7}],Table[{i-1,7},{i,7,1,-1}],Table[{0,i-1},{i,7,1,-1}]]},With[{G=RegionCentroid[Polygon[P]]},With[{Q=Map[transform[#,58Degree,G]&,P]},Graphics[{{Gray,Dashed,Line[{P[[28]],P[[7]],P[[14]],P[[21]],P[[28]]}],Pink,Line[{Q[[28]],Q[[7]],Q[[14]],Q[[21]],Q[[28]]}]},Table[Line[{P[[i]],Q[[i]]}],{i,1,28}],PointSize[0.03],Point[P],Pink,Point[Q]}]]]]

Tune the graphical aspect of the result.

With[{P=Join[Table[{i,0},{i,1,7}],Table[{7,j},{j,1,7}],Table[{i-1,7},{i,7,1,-1}],Table[{0,j-1},{j,7,1,-1}]]},With[{G=RegionCentroid[Polygon[P]]},With[{Q=Map[tr[#,58Degree,G]&,P]},Graphics[{{Blue,Line[{P[[28]],P[[7]],P[[14]],P[[21]],P[[28]]}]},{Red,Line[{Q[[28]],Q[[7]],Q[[14]],Q[[21]],Q[[28]]}]},{Table[Line[{P[[i]],Q[[i]]}],{i,1,28}]},Point[G],PointSize[0.02],
{Blue,Point[P]},{Red,Point[Q]}}]]]]

Making the angle generic

Define a function to build the drawing (with angle as a parameter).

drawing[a_]:=With[{P=Join[Table[{i,0},{i,1,7}],Table[{7,i},{i,1,7}],Table[{i-1,7},{i,7,1,-1}],Table[{0,i-1},{i,7,1,-1}]]},With[{G=RegionCentroid[Polygon[P]]},With[{Q=Map[transform[#,a,G]&,P]},Graphics[{{Blue,Line[{P[[28]],P[[7]],P[[14]],P[[21]],P[[28]]}],Red,Line[{Q[[28]],Q[[7]],Q[[14]],Q[[21]],Q[[28]]}]},Table[Line[{P[[i]],Q[[i]]}],{i,1,28}],PointSize[0.02],{Blue,Point[P]},{Red,Point[Q]}},PlotRange->{{-1.75,8.75},{-1.75,8.75}}]]]]

Test it.

drawing[120Degree]

Develop an interactive version to play with the value of the angle..

Manipulate[drawing[α],{α,0,2Pi}]

Making the number of points generic

Define a function to build the drawing (with angle and number of points on each edge as parameters).

drawing[a_,n_]:=With[{P=Join[Table[{i,0},{i,1,n}],Table[{n,i},{i,1,n}],Table[{i-1,n},{i,n,1,-1}],Table[{0,i-1},{i,n,1,-1}]]},With[{G=RegionCentroid[Polygon[P]]},With[{Q=Map[transform[#,a,G]&,P]},Graphics[{{Blue,Line[{P[[4n]],P[[n]],P[[2n]],P[[3n]],P[[4n]]}],Red,Line[{Q[[4n]],Q[[n]],Q[[2n]],Q[[3n]],Q[[4n]]}]},Table[Line[{P[[i]],Q[[i]]}],{i,1,4n}],PointSize[0.02],{Blue,Point[P]},{Red,Point[Q]}},PlotRange->{{-n/4,5n/4},{-n/4,5n/4}}]]]]

drawing[58Degree,7]

Develop an interactive version to play with the values of the angle and number of points.

Manipulate[drawing[α,n],{α,0,2Pi},{n,2,20,1}]

Making the polygon (more) generic

Extend what has been done to regular polygons with different number of edges.

Constructing the list of points

V={v1,v2,v3,v4}

Table[v1+i(v2-v1)/5,{i,1,5}]

Table[v2+i(v3-v2)/5,{i,1,5}]

Partition[V,2,1,{1,1}]

Apply[Join,Map[Table[#[[1]]+i(#[[2]]-#[[1]])/5,{i,1,5}]&,Partition[V,2,1,{1,1}]]]

CirclePoints[4]

Apply[Join,Map[Table[#[[1]]+i(#[[2]]-#[[1]])/5,{i,1,5}]&,Partition[CirclePoints[4],2,1,{1,1}]]]

Graphics[{PointSize[0.02],Point[Apply[Join,Map[Table[#[[1]]+i(#[[2]]-#[[1]])/3,{i,1,3}]&,Partition[CirclePoints[6],2,1,{1,1}]]]]}]

Injecting in the drawing

drawing[a_,n_,v_]:=With[{P=Apply[Join,Map[Table[#[[1]]+i(#[[2]]-#[[1]])/n,{i,1,n}]&,Partition[CirclePoints[v],2,1,{1,1}]]]},With[{G=RegionCentroid[Polygon[P]]},With[{Q=Map[transform[#,a,G]&,P]},Graphics[{{Blue,Line[Prepend[Table[P[[i n]],{i,1,v}],P[[v n]]]],Red,Line[Prepend[Table[Q[[i n]],{i,1,v}],Q[[v n]]]]},Table[Line[{P[[i]],Q[[i]]}],{i,1,v n}],PointSize[0.02],{Blue,Point[P]},{Red,Point[Q]}},PlotRange->{{-1,1},{-1,1}}]]]]

drawing[Pi/3,7,4]

Manipulate[drawing[α,n,v],{α,0,2Pi},{n,2,20,1},{v,3,8,1}]