11// original algorithm Xenowhirl
22// stole code from ChaseMor/pxt-arcade-rotsprite
3- // generated the rest with chatGPT
3+ // some generated with chatGPT
44
55export default function rotSprite ( image , angle ) {
66 image = scale2xImage ( image ) ;
@@ -19,6 +19,7 @@ function scale2xImage(original) {
1919 const b = original . getPixel ( x + 1 , y ) ;
2020 const c = original . getPixel ( x - 1 , y ) ;
2121 const d = original . getPixel ( x , y + 1 ) ;
22+
2223 if ( c == a && c != d && a != b ) {
2324 scaled . setPixel ( x << 1 , y << 1 , a ) ;
2425 } else {
@@ -43,6 +44,7 @@ function scale2xImage(original) {
4344 }
4445 return scaled ;
4546}
47+
4648function rotateAndReduceImage ( original , angle ) {
4749 let rotated = Pixels . create ( original . width >> 3 , original . height >> 3 ) ;
4850
@@ -112,4 +114,67 @@ export class Pixels {
112114 }
113115 return clonedImage ;
114116 } ;
117+
118+ scale = ( factor ) => {
119+ // Calculate the new dimensions of the scaled image
120+ const scaledWidth = this . width * factor ;
121+ const scaledHeight = this . height * factor ;
122+
123+ // Create a new Image object with the new dimensions
124+ const scaledImage = new Pixels ( scaledWidth , scaledHeight ) ;
125+
126+ // Loop through the pixels of the scaled image
127+ for ( let y = 0 ; y < scaledHeight ; y ++ ) {
128+ for ( let x = 0 ; x < scaledWidth ; x ++ ) {
129+ // Calculate the coordinates of the corresponding pixel in the original image
130+ const originalX = Math . floor ( x / factor ) ;
131+ const originalY = Math . floor ( y / factor ) ;
132+
133+ // Set the pixel in the scaled image to the nearest pixel in the original image
134+ scaledImage . setPixel ( x , y , this . getPixel ( originalX , originalY ) ) ;
135+ }
136+ }
137+
138+ return scaledImage ;
139+ } ;
140+ }
141+
142+
143+ export function addMarginToImageData ( ctx , imageData , xMargin , yMargin ) {
144+ // Create a new ImageData object with the new dimensions, including the margin.
145+ const newImageData = ctx . createImageData (
146+ imageData . width + xMargin * 2 ,
147+ imageData . height + yMargin * 2
148+ ) ;
149+
150+ // Loop through the original image data and copy the pixel values to the new
151+ // ImageData object, taking the margin into account.
152+ for ( let row = 0 ; row < imageData . height ; row ++ ) {
153+ for ( let col = 0 ; col < imageData . width ; col ++ ) {
154+ const sourcePixel = [ imageData . data [ ( row * imageData . width + col ) * 4 + 0 ] ,
155+ imageData . data [ ( row * imageData . width + col ) * 4 + 1 ] ,
156+ imageData . data [ ( row * imageData . width + col ) * 4 + 2 ] ,
157+ imageData . data [ ( row * imageData . width + col ) * 4 + 3 ]
158+ ] ;
159+
160+ const destRow = row + yMargin ;
161+ const destCol = col + xMargin ;
162+ for ( let i = 0 ; i < 4 ; i ++ ) {
163+ newImageData . data [ ( destRow * newImageData . width + destCol ) * 4 + i ] =
164+ sourcePixel [ i ] ;
165+ }
166+ }
167+ }
168+
169+ return newImageData ;
170+ }
171+
172+ export function getRotateDiagonal ( width , height ) {
173+ const diagonal =
174+ 1 + ( 0 | ( 2 * Math . sqrt ( width ** 2 / 4 + height ** 2 / 4 ) ) ) ;
175+
176+ const xMargin = Math . round ( ( diagonal - width ) / 2 ) ;
177+ const yMargin = Math . round ( ( diagonal - height ) / 2 ) ;
178+
179+ return { diagonal : xMargin * 2 + width , xMargin, yMargin } ;
115180}
0 commit comments