forked from HubiAtGit/Logo
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlogoturtle.Rmd
More file actions
155 lines (116 loc) · 3.32 KB
/
logoturtle.Rmd
File metadata and controls
155 lines (116 loc) · 3.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
---
title: "logoturtle"
author: "Luis Britz"
date: "2025-05-21"
output: html_document
---
# Logo Project
## initialize turtle state and some generic functions
```{R}
turtle <- new.env()
init_turtle <- function(xlim = c(-50, 50), ylim = c(-50, 50)) {
turtle$position <- c(0, 0)
turtle$heading <- 90
turtle$pen <- TRUE
turtle$visible <- TRUE
turtle$history <- list()
turtle$xlim <- xlim
turtle$ylim <- ylim
# Initialize the plot
plot(NA, xlim = xlim, ylim = ylim, type = "n", asp = 1,
xlab = "", ylab = "", axes = FALSE, main = "LOGO")
draw_turtle()
}
draw_turtle <- function() {
if (!turtle$visible) return()
# access current state
x <- turtle$position[1]
y <- turtle$position[2]
angle <- turtle$heading
# turtle shape size
size <- 10 # Size of the turtle
radians <- function(deg) deg * pi / 180
# calculate shape points
p1 <- c(x, y)
p2 <- c(x + size * cos(radians(angle - 135)),
y + size * sin(radians(angle - 135)))
p3 <- c(x + size * cos(radians(angle + 135)),
y + size * sin(radians(angle + 135)))
# draw triangle
polygon(x = c(p1[1], p2[1], p3[1]), y = c(p1[2], p2[2], p3[2]),
col = "darkgreen", border = "black")
}
redraw <- function() {
plot(NA, xlim = turtle$xlim, ylim = turtle$ylim, type = "n", asp = 1,
xlab = "", ylab = "", axes = FALSE, main = "Turtle Graphics in R")
if (length(turtle$history) > 0) {
segments(
x0 = sapply(turtle$history, `[`, 1),
y0 = sapply(turtle$history, `[`, 2),
x1 = sapply(turtle$history, `[`, 3),
y1 = sapply(turtle$history, `[`, 4)
)
}
draw_turtle()
}
```
using sapply() instead of a for loop to optimize the code!
## moving functions
```{R}
move_turtle <- function(steps, redraw = TRUE) {
x0 <- turtle$position[1]
y0 <- turtle$position[2]
angle_rad <- turtle$heading * pi / 180
x1 <- x0 + steps * cos(angle_rad)
y1 <- y0 + steps * sin(angle_rad)
if (turtle$pen) {
turtle$history[[length(turtle$history) + 1]] <- c(x0, y0, x1, y1)
}
turtle$position <- c(x1, y1)
if (redraw) redraw()
}
fd <- function(steps, redraw = TRUE) move_turtle(steps, redraw = redraw)
bk <- function(steps, redraw = TRUE) move_turtle(-steps, redraw = redraw)
lt <- function(degrees, redraw = TRUE) {
turtle$heading <- (turtle$heading + degrees) %% 360
if (redraw) redraw()
}
rt <- function(degrees, redraw = TRUE) {
turtle$heading <- (turtle$heading - degrees) %% 360
if (redraw) redraw()
}
```
the functions to actually move the turtle. i added a redraw argument, because the plotting every single step took too long and threw off the calculations to produce crooked shapes... i really liked this solution, because i figured it out completely without chatgpts help!
## generating the logo code
R equivalent:
```{R}
init_turtle()
for (i in 1:20){
for (j in 1:180) {
fd(1, redraw = FALSE)
rt(2, redraw = FALSE)
}
rt(18, redraw = FALSE)
}
redraw()
```
## other functions
```{R}
home <- function() {
x0 <- turtle$position[1]
y0 <- turtle$position[2]
x1 <- 0
y1 <- 0
if (turtle$pen) {
turtle$history[[length(turtle$history) + 1]] <- c(x0, y0, x1, y1)
}
turtle$position <- c(x1, y1)
redraw()
}
clearscreen <- function() {
turtle$history <- list()
turtle$position <- turtle$home
turtle$heading <- 90
redraw()
}
```