Skip to content

Commit 157f2cb

Browse files
author
algorithmica-repository
committed
Uploading matrix transformations example
1 parent 2898f90 commit 157f2cb

File tree

1 file changed

+31
-0
lines changed

1 file changed

+31
-0
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
draw.segment <- function(p1,p2,color,width=3) {
2+
segments(p1[1],p1[2],p2[1],p2[2], col=color, lwd=width)
3+
}
4+
5+
plot(NULL,xlim=c(-3,3),ylim=c(-3,3),ylab="y",xlab="x")
6+
7+
# draw a segment (initially at 45 degrees)
8+
p.init <- matrix(c(0,0),2,1)
9+
p.end <- matrix(c(2,2),2,1)
10+
draw.segment(p.init, p.end, "red")
11+
12+
# rotation example
13+
theta <- pi/4 # rotate more 45º degrees
14+
rotate.matrix <- matrix(c( cos(theta),sin(theta),
15+
-sin(theta),cos(theta)),2,2)
16+
17+
p1.init <- rotate.matrix %*% p.init
18+
p1.end <- rotate.matrix %*% p.end
19+
draw.segment(p1.init, p1.end, "blue")
20+
21+
# scaling example
22+
scalingx <- 0.5 # scale both axis by 50%
23+
scalingy <- 0.5
24+
scale.matrix <- matrix(c(scalingx, 0,
25+
0, scalingy),2,2)
26+
27+
p2.init <- scale.matrix %*% p.init
28+
p2.end <- scale.matrix %*% p.end
29+
draw.segment(p2.init, p2.end, "green")
30+
31+
plot(NULL,xlim=c(-3,3),ylim=c(-3,3),ylab="y",xlab="x")

0 commit comments

Comments
 (0)