@@ -7,21 +7,21 @@ const DOM_CANVAS = document.querySelector("canvas");
77const SPACE_COLOR = [ 63 , 63 , 70 ] ;
88const BASIS1_COLOR = [ 239 , 68 , 68 ] ;
99const BASIS2_COLOR = [ 34 , 197 , 94 ] ;
10+ const GRID_COLOR = [ 3 , 7 , 18 ] ;
1011
1112// Values
1213
1314let CANVAS_HAS_CHANGED = true ;
1415let SCALE = 50 ;
15- let BASIS = [
16- [ [ 1 ] , [ 0 ] ] , // basis 1
17- [ [ 0 ] , [ 1 ] ] // bassi 2
18- ] ;
16+ let BASIS = [ null , null ] ;
1917
2018
2119// P5
2220
2321function setup ( ) {
2422 createCanvas ( windowWidth + 200 , windowHeight + 200 , P2D , DOM_CANVAS ) ;
23+ BASIS [ 0 ] = createVector ( 1 , 0 ) ;
24+ BASIS [ 1 ] = createVector ( 0 , 1 ) ;
2525}
2626
2727function draw ( ) {
@@ -33,6 +33,7 @@ function draw() {
3333 scale ( 1 , - 1 ) ;
3434
3535 background ( SPACE_COLOR ) ;
36+ drawBasisGrid ( BASIS , GRID_COLOR ) ;
3637 drawBasisArrows ( BASIS ) ;
3738
3839 CANVAS_HAS_CHANGED = false ;
@@ -46,8 +47,8 @@ function windowResized() {
4647// Canvas utils
4748
4849function drawBasisArrows ( basis ) {
49- drawArrow ( 0 , 0 , basis [ 0 ] [ 0 ] , basis [ 0 ] [ 1 ] , BASIS1_COLOR ) ;
50- drawArrow ( 0 , 0 , basis [ 1 ] [ 0 ] , basis [ 1 ] [ 1 ] , BASIS2_COLOR ) ;
50+ drawArrow ( 0 , 0 , basis [ 0 ] . x , basis [ 0 ] . y , BASIS1_COLOR ) ;
51+ drawArrow ( 0 , 0 , basis [ 1 ] . x , basis [ 1 ] . y , BASIS2_COLOR ) ;
5152}
5253
5354function drawArrow ( x1 , y1 , x2 , y2 , color ) {
@@ -73,3 +74,36 @@ function drawArrow(x1, y1, x2, y2, color) {
7374
7475 triangle ( x2 , y2 , x3 , y3 , x4 , y4 ) ;
7576}
77+
78+ function drawBasisGrid ( basis , color ) {
79+ stroke ( color ) ;
80+
81+ let cols = ceil ( windowWidth / 2 ) ;
82+ let rows = ceil ( windowHeight / 2 ) ;
83+
84+ for ( let i = - cols ; i <= cols ; i ++ ) {
85+ strokeWeight ( ( i === 0 ) ? 2 : 1 ) ;
86+ let start = p5 . Vector . mult ( basis [ 1 ] , - rows )
87+ . add ( p5 . Vector . mult ( basis [ 0 ] , i ) )
88+ . mult ( SCALE ) ;
89+
90+ let end = p5 . Vector . mult ( basis [ 1 ] , rows )
91+ . add ( p5 . Vector . mult ( basis [ 0 ] , i ) )
92+ . mult ( SCALE ) ;
93+
94+ line ( start . x , start . y , end . x , end . y ) ;
95+ }
96+
97+ for ( let j = - rows ; j <= rows ; j ++ ) {
98+ strokeWeight ( ( j === 0 ) ? 2 : 1 ) ;
99+ let start = p5 . Vector . mult ( basis [ 0 ] , - cols )
100+ . add ( p5 . Vector . mult ( basis [ 1 ] , j ) )
101+ . mult ( SCALE ) ;
102+
103+ let end = p5 . Vector . mult ( basis [ 0 ] , cols )
104+ . add ( p5 . Vector . mult ( basis [ 1 ] , j ) )
105+ . mult ( SCALE ) ;
106+
107+ line ( start . x , start . y , end . x , end . y ) ;
108+ }
109+ }
0 commit comments