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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194

use std::f32::consts::PI;


pub fn display_rotated_array( amounttorotate: f32 ) {
    
    let mut square : Vec<Vec<u32>> = Vec::new();
    let mut newsquare: Vec<Vec<u32>> = Vec::new();
    let mut counter = 100;
    
    //filling the new square with "888" to represent values that are not mapped to after rotating square
    for x in 0..9{
        let mut newsquarevec = Vec::new();
        for y in 0..9{
            newsquarevec.push(888);
        }
        newsquare.push(newsquarevec);
    }
    
    //filling square with 100 - 180 and newsquare by rotating each point of the old square
    for x in 0..9{
        
        let mut squarevec = Vec::new();
        
        for y in 0..9{
            
            squarevec.push(counter);
            
            let newpos = ortho_rotate_i8_point_at_point( (x, y) , (4,4), amounttorotate);
            
            let vecx = newpos.0 as usize;
            let vecy = newpos.1 as usize;
            
            newsquare[vecx][vecy] = counter;
            
            counter += 1;
        };
        
        square.push(squarevec);
    };
    
    
    print_2d_vector(square);
    println!("");
    print_2d_vector(newsquare);   
    
}


//print a 2d vector of u32s
fn print_2d_vector(vec: Vec<Vec<u32>>){
    
    for x in vec{
        for y in x{
            print!(" {:?} ", y);
        }
        println!("");
    }
}


//turn the point into 
pub fn ortho_rotate_i8_point(point: (i8,i8), torotate: f32 ) -> (i8,i8){
    
    let floatpoint = (point.0 as f32, point.1 as f32);
    
    let rotatedfloatpoint = ortho_rotate_point_at_origin(floatpoint, torotate);
    
    let rotatedi8point = (rotatedfloatpoint.0.round() as i8, rotatedfloatpoint.1.round() as i8);
    
    
    return rotatedi8point;
}


pub fn ortho_rotate_i8_point_at_point(point: (i8,i8), origin: (i8,i8), torotate: f32 ) -> (i8,i8){
    
    let pointprime = (point.0 - origin.0, point.1 - origin.1);
    
    let rotatedpoint = ortho_rotate_i8_point(pointprime, torotate);
    
    let rotatedpointprime = (rotatedpoint.0 + origin.0, rotatedpoint.1 + origin.1);
    
    return rotatedpointprime;
}


//rotate a point with another point as its origin
pub fn ortho_rotate_point_at_point( point: (f32,f32), originpoint: (f32,f32), torotate: f32) -> (f32,f32){
    
    
    //move the point so that the origin is the originpoint
    let pointprime = (point.0 - originpoint.0, point.1 - originpoint.1);
    
    let pointprime2 = ortho_rotate_point_at_origin(point, torotate);
    
    let pointprime3 = (pointprime2.0 + originpoint.0, pointprime2.1 + originpoint.1);
    
    return pointprime3;
}

//rotate this point by this amount and return it
pub fn ortho_rotate_point_at_origin( point: (f32,f32), torotate: f32) -> (f32,f32){
    
    if point.0 == point.1  && point.0 == 0.0{
        return (0.0,0.0) ;
    }
    
    for numrotation in 0..4{
        
        //rotate the point clockwise x times (negative direction)
        let (rotx,roty) = rotate_point_90_degrees_x_times( point , - (numrotation as i8) );
        
        if roty<rotx && roty>=-rotx {
            
            let sidelength = rotx * 2.0;
            
            //how much the position in the current section contributes to the total rotation
            let sectioncurrotation = ((roty / sidelength)+0.5 ) / 4.0;
            
            //add the amount this is already rotated to the amount to rotate
            let torotate = torotate + (numrotation as f32 * 0.25) + sectioncurrotation;
            
            
            
            //how many blocks (full 90 degree rotations) to apply to the newposprime2
            let blockstorotate = (torotate / 0.25).floor() as i8;
            
            //how much the section will contribute to the total rotation
            let sectiontorotate = torotate % 0.25;
            
            
            //point at bottom right corner
            let newposprime = (sidelength/2.0, -sidelength/2.0);
            
            //bottom right corner plus how much of a rotation is needed for this section * sidelength
            let newposprime2 = (newposprime.0, newposprime.1 + sectiontorotate * sidelength *4.0);
            
            
            
            let newpos = rotate_point_90_degrees_x_times(  newposprime2  , blockstorotate);
            
            return newpos;
            
        };
    };
    
    panic!("something went wrong");
}



fn rotate_point_90_degrees_clockwise( point: (f32,f32)) -> (f32,f32){
    //90 degrees clockwise rotation matrix (negative)
    //xprime = (0,  1)  = point.1
    //yprime = (-1, 0)  = -point.0
    
    return (point.1, -point.0);
}

fn rotate_point_90_degrees_counter_clockwise( point: (f32,f32)) -> (f32,f32){
    //90 degrees counter clockwise rotation matrix (positive)
    //xprime = (0, -1) = -point.1
    //yprime = (1,  0) = point.0
    
    return (-point.1, point.0);    
}


//how much to rotate this point in the positive rotation direction (counter clockwise)
fn rotate_point_90_degrees_x_times( mut point: (f32,f32), mut times: i8 ) -> (f32,f32){
    
    while times != 0{
        
        //rotate in the positive direction
        if times > 0{
            
            point = rotate_point_90_degrees_counter_clockwise(point);
            
            times += -1;
        }
        
        //rotate in the negative direction
        if times < 0{
            
            point = rotate_point_90_degrees_clockwise(point);
            
            times += 1;
        }
    }
    
    
    return point;
}