This repository was archived by the owner on Jul 5, 2022. It is now read-only.
File tree 4 files changed +118
-0
lines changed
CodingChallenges/CC_121_Logo/P5
4 files changed +118
-0
lines changed Original file line number Diff line number Diff line change
1
+ <!DOCTYPE html>
2
+ < html >
3
+
4
+ < head >
5
+ < meta charset ="UTF-8 ">
6
+ < meta http-equiv ="X-UA-Compatible " content ="IE=edge ">
7
+ < meta name ="viewport " content ="width=device-width, initial-scale=1 ">
8
+ < title > Logo</ title >
9
+ < script src ="https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.7.2/p5.min.js "> </ script >
10
+ < script src ="https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.7.2/addons/p5.dom.min.js "> </ script >
11
+ < script src ="turtle.js "> </ script >
12
+ < script src ="sketch.js "> </ script >
13
+ </ head >
14
+
15
+ < body >
16
+ < textarea id ="code " cols ="40 " rows ="5 ">
17
+ fd 60 rt 120 fd 60 rt 120 fd 60 rt 120
18
+ </ textarea >
19
+ </ body >
20
+
21
+ </ html >
Original file line number Diff line number Diff line change
1
+ // Daniel Shiffman
2
+ // http://youtube.com/thecodingtrain
3
+ // http://codingtra.in
4
+
5
+ // Coding Challenge 121: Logo
6
+ // https://youtu.be/i-k04yzfMpw
7
+
8
+ let editor ;
9
+ let turtle ;
10
+
11
+ function setup ( ) {
12
+ createCanvas ( 200 , 200 ) ;
13
+ angleMode ( DEGREES ) ;
14
+ background ( 0 ) ;
15
+ turtle = new Turtle ( 100 , 100 , 0 ) ;
16
+ editor = select ( '#code' ) ;
17
+ editor . input ( goTurtle ) ;
18
+ goTurtle ( ) ;
19
+ }
20
+
21
+ function goTurtle ( ) {
22
+ background ( 0 ) ;
23
+ push ( ) ;
24
+ turtle . reset ( ) ;
25
+ let code = editor . value ( ) ;
26
+ let tokens = code . split ( ' ' ) ;
27
+ let index = 0 ;
28
+ while ( index < tokens . length ) {
29
+ let token = tokens [ index ] ;
30
+ if ( commands [ token ] ) {
31
+ if ( token . charAt ( 0 ) === 'p' ) {
32
+ commands [ token ] ( ) ;
33
+ } else {
34
+ commands [ token ] ( tokens [ ++ index ] ) ;
35
+ }
36
+ }
37
+ index ++ ;
38
+ }
39
+ pop ( ) ;
40
+ }
Original file line number Diff line number Diff line change
1
+ // Daniel Shiffman
2
+ // http://youtube.com/thecodingtrain
3
+ // http://codingtra.in
4
+
5
+ // Coding Challenge 121: Logo
6
+ // https://youtu.be/i-k04yzfMpw
7
+
8
+ const commands = {
9
+ fd : function ( amt ) {
10
+ turtle . forward ( amt ) ;
11
+ } ,
12
+ bd : function ( amt ) {
13
+ turtle . forward ( - amt ) ;
14
+ } ,
15
+ rt : function ( angle ) {
16
+ turtle . right ( angle ) ;
17
+ } ,
18
+ lt : function ( angle ) {
19
+ turtle . right ( - angle ) ;
20
+ } ,
21
+ pu : function ( ) {
22
+ turtle . pen = false ;
23
+ } ,
24
+ pd : function ( ) {
25
+ turtle . pen = true ;
26
+ }
27
+ } ;
28
+
29
+ class Turtle {
30
+ constructor ( x , y , angle ) {
31
+ this . x = x ;
32
+ this . y = y ;
33
+ this . dir = angle ;
34
+ }
35
+
36
+ reset ( ) {
37
+ console . log ( this . x , this . y , this . dir ) ;
38
+ translate ( this . x , this . y ) ;
39
+ rotate ( this . dir ) ;
40
+ this . pen = true ;
41
+ }
42
+
43
+ forward ( amt ) {
44
+ amt = parseInt ( amt ) ;
45
+ if ( this . pen ) {
46
+ stroke ( 255 ) ;
47
+ strokeWeight ( 2 ) ;
48
+ line ( 0 , 0 , amt , 0 ) ;
49
+ }
50
+ translate ( amt , 0 ) ;
51
+ }
52
+
53
+ right ( angle ) {
54
+ rotate ( angle ) ;
55
+ }
56
+ }
Original file line number Diff line number Diff line change 2
2
title : " Logo Interpreter"
3
3
video_number : 121
4
4
date : 2018-10-30
5
+ repository : CC_121_Logo
5
6
video_id : i-k04yzfMpw
6
7
7
8
links :
You can’t perform that action at this time.
0 commit comments