source("http://macosa.dima.unige.it/r.R")    # If I have not already loaded the library
---------------------------------------------------------------------------------------
# I can shift with a translation, rotate and multiply the points by:
# MulTraRot(x,y, mx,my, tx,ty, ang) and RotTraMul(x,y, ang, tx,ty, mx,my).
# The first multiply the coordinates by mx and my, then shift by tx,ty and finally
# rotates the point about (0,0) by ang. The second produces first a rotation about (0,0),
# then a translation and finally a multiplication of the coordinates. If I add [1] or
# [2] to the command I have the 1st or the 2nd coordinate.  An example:
#
HF=5; BF=5
x <- c(13,11,10, 9, 7,7.5, 7, 9,10,11,13,17,20,20.5,18.5,21,20,17,13)
y <- c(17,16,15,13,10, 13,16,13,11,10, 9, 9,11,  12,  13,13,15,17,17)
c <- "black"
n = length(x)
PLANE(-25,30,-20,35)
# The yellow fish, with black border
polyC(x,y,"yellow"); polyline(x,y,c)
  
xx = yy = 0     # I "initialize" xx and yy
# Symmetry with respect to the x axis (brown border)
for(i in 1:n) {xx[i]=y[i]; yy[i]=x[i]}
polyline(xx,yy, "brown")
# Reverse around the x axis (red border)
for(i in 1:n) {xx[i]=x[i]; yy[i]=-y[i]}
polyline(xx,yy, "red")
# Reverse around the y axis (seagreen border)
for(i in 1:n) {xx[i]=-x[i]; yy[i]=y[i]}
polyline(xx,yy, "seagreen")
# Scale reduction (light green)
for(i in 1:n) {xx[i]=x[i]/2; yy[i]=y[i]/2}
polyline(xx,yy, "green")
line(0,0, x[7],y[7], "red"); line(0,0, x[12],y[12], "red")
# Rotation of light green fish of 90 ° around -1,1 (orange border)
# First move it by bringing -1,1 into the origin, then rotate and move it in the
# opposite direction
POINT(-1,1, "blue")
for(i in 1:n) {xx[i]=xx[i]+1; yy[i]=yy[i]-1}
for(i in 1:n) {k = RotTraMul(xx[i],yy[i], 90, -1,1, 1,1); xx[i]=k[1]; yy[i]=k[2]}
polyline(xx,yy, "orange")
line(-1,1, x[5]/2,y[5]/2, "blue"); line(-1,1, xx[5],yy[5], "blue")
# Halving of the x and doubling of the y of the initial fish (magenta)
for(i in 1:n) {xx[i]=x[i]/2; yy[i]=y[i]*2}
polyline(xx,yy, "magenta")
# Similarity and overturn (gray fish)
for(i in 1:n) {xx[i]=y[i]*2-40; yy[i]=x[i]*2-10}
polyline(xx,yy, "grey")
#
#
# RotP(x,y, ang, x0,y0) rotates (x,y) about (x0,y0) by ang°.
# RotPx(x,y, ang, x0,y0), RotPy(x,y, ang, x0,y0) give his two coordinates.
#
HF=4; BF=4
x = c(13,11,10, 9, 7,7.5, 7, 9,10,11,13,17,20,20.5,18.5,21,20,17,13)
y = c(17,16,15,13,10, 13,16,13,11,10, 9, 9,11,  12,  13,13,15,17,17)
n = length(x); a=5; b=20
PLANE(-15,25, 0,40)
polyC(x,y,"yellow"); POINT(a,b,"blue"); x1=NULL; y1=NULL
for(i in 1:n) x1[i]=RotPx(x[i],y[i],360/5,a,b); for(i in 1:n) y1[i]=RotPy(x[i],y[i],360/5,a,b)
polyC(x1,y1,"yellow")
for(i in 1:n) x1[i]=RotPx(x[i],y[i],360/5*2,a,b); for(i in 1:n) y1[i]=RotPy(x[i],y[i],360/5*2,a,b)
polyC(x1,y1,"yellow")
for(i in 1:n) x1[i]=RotPx(x[i],y[i],360/5*3,a,b); for(i in 1:n) y1[i]=RotPy(x[i],y[i],360/5*3,a,b)
polyC(x1,y1,"yellow")
for(i in 1:n) x1[i]=RotPx(x[i],y[i],360/5*4,a,b); for(i in 1:n) y1[i]=RotPy(x[i],y[i],360/5*4,a,b)
polyC(x1,y1,"yellow")
                     
#
# Here's how to create the figure below to the left.
HF=4; BF=4
x_1 <- c(13,11,10, 9, 7,7.5, 7, 9,10,11,13,17,20,20.5,18.5,21,20,17,13)
y_1 <- c(17,16,15,13,10, 13,16,13,11,10, 9, 9,11,  12,  13,13,15,17,17)
x_2 <- c(13,12,15,14,17,13)
y_2 <- c(17,20,17,20,17,17)
x_3 <- c(17,14,15,12,13,17)
y_3 <- c( 9, 6, 9, 6, 9, 9)
c_1 <- "black"; c_2 <- "green"; c_3 <- "magenta"
n1 = length(x_1); n2 = length(x_2); n3 = length(x_3)
PLANE(-30,30,-30,30)
# The yellow fish.
polyC(x_1,y_1,"yellow")
polyline(x_1,y_1,c_1); polyline(x_2,y_2,c_2); polyline(x_3,y_3,c_3)
# The transformation that turns the fish 60° around 0,0
xx_1=xx_2=xx_3=yy_1=yy_2=yy_3=0     # inzializzo le variabili
for(i in 1:n1) {k = MulTraRot(x_1[i],y_1[i], 1,1, 0,0, 60); xx_1[i]=k[1]; yy_1[i]=k[2]}
for(i in 1:n2) {k = MulTraRot(x_2[i],y_2[i], 1,1, 0,0, 60); xx_2[i]=k[1]; yy_2[i]=k[2]}
for(i in 1:n3) {k = MulTraRot(x_3[i],y_3[i], 1,1, 0,0, 60); xx_3[i]=k[1]; yy_3[i]=k[2]}
polylin(xx_1,yy_1, c_1); polylin(xx_2,yy_2,c_2); polylin(xx_3,yy_3,c_3)
# I squash the fish horizontally and stretch it vertically, do not move it, rotate it
# 180° around 0,0
for(i in 1:n1) {k = MulTraRot(x_1[i],y_1[i], 1/2,1.5, 0,0, 180); xx_1[i]=k[1]; yy_1[i]=k[2]}
for(i in 1:n2) {k = MulTraRot(x_2[i],y_2[i], 1/2,1.5, 0,0, 180); xx_2[i]=k[1]; yy_2[i]=k[2]}
for(i in 1:n3) {k = MulTraRot(x_3[i],y_3[i], 1/2,1.5, 0,0, 180); xx_3[i]=k[1]; yy_3[i]=k[2]}
polylin(xx_1,yy_1, c_1); polylin(xx_2,yy_2,c_2); polylin(xx_3,yy_3,c_3)
# I rotate it around 0,0 by -90°, move it by -15,15, doubling the abscissa 
for(i in 1:n1) {k = RotTraMul(x_1[i],y_1[i], -90, -15,15, 2,1); xx_1[i]=k[1]; yy_1[i]=k[2]}
for(i in 1:n2) {k = RotTraMul(x_2[i],y_2[i], -90, -15,15, 2,1); xx_2[i]=k[1]; yy_2[i]=k[2]}
for(i in 1:n3) {k = RotTraMul(x_3[i],y_3[i], -90, -15,15, 2,1); xx_3[i]=k[1]; yy_3[i]=k[2]}
polylin(xx_1,yy_1, c_1); polylin(xx_2,yy_2,c_2); polylin(xx_3,yy_3,c_3)
# Rotate the object by -70°, do not move it, decrease the abscissa and increase the ordinates
for(i in 1:n1) {k = RotTraMul(x_1[i],y_1[i], -70, 0,0, 0.75,1.25); xx_1[i]=k[1]; yy_1[i]=k[2]}
for(i in 1:n2) {k = RotTraMul(x_2[i],y_2[i], -70, 0,0, 0.75,1.25); xx_2[i]=k[1]; yy_2[i]=k[2]}
for(i in 1:n3) {k = RotTraMul(x_3[i],y_3[i], -70, 0,0, 0.75,1.25); xx_3[i]=k[1]; yy_3[i]=k[2]}
polylin(xx_1,yy_1, c_1); polylin(xx_2,yy_2,c_2); polylin(xx_3,yy_3,c_3)
 
  
 
# After tracing a segment or arrow (with arrow, segm, line, … commands), the end
# point coordinates are stored in xend and yend. This may be useful in building
# various types of figures. An initial point can be plotted with Dot: its coordinates
# are put into xend, yend. See below how to generate the figure above to the right.
 
HF=3; BF=3; PIANO(-1,1,-1,1)
Dot(1,0, 1)
for(i in 1:500) {P = RotTraMul(xend*99/100,yend*99/100, 30, 0,0, 1,1); segm(xend,yend, P[1],P[2], i) }
#
# MulRotP(x,y, m, ang, x0,y0) multiply by m the distance of (x,y) from (x0,y0) and
# rotates it about (x0,y0) by ang°. An example:
PLANE(0,10,0,10)
polyC(c(3,7,3,3),c(4,4,7,4),"yellow")
polyline(c(3,7,3,3),c(4,4,7,4),"brown")
x=c(7,3,2,4,5,7); y=c(4,4,2,2,1,4)
polyline(x,y,"brown"); text(4.5,2.5,"A")
x0=y0=0; x1=y1=0
for(i in 1:6) {x0[i]=x[i]-4; y0[i]=y[i]}
for(i in 1:6) {x1[i]=MulRotP(x0[i],y0[i],3/4,-90, 3,4)[1];
               y1[i]=MulRotP(x0[i],y0[i],3/4,-90, 3,4)[2]}
polyline(x1,y1,"brown"); text(2.4,5.5,"B")
for(i in 1:6) {x0[i]=x[i]-4; y0[i]=y[i]+3}
a = -180-atan(3/4)*180/pi
for(i in 1:6) {x1[i]=MulRotP(x0[i],y0[i],5/4,a, 3,7)[1];
               y1[i]=MulRotP(x0[i],y0[i],5/4,a, 3,7)[2]}
polyline(x1,y1,"brown"); text(5.5,6.5,"C")

                     
#   [  Area of A is 8.5, area of C is 8.5*(5/4)^2 = 425/32  ]

back