|
| 1 | +// original algorithm Xenowhirl |
| 2 | +// stole code from ChaseMor/pxt-arcade-rotsprite |
| 3 | +// generated the rest with chatGPT |
| 4 | + |
| 5 | +export default function rotSprite(image, angle) { |
| 6 | + image = scale2xImage(image); |
| 7 | + image = scale2xImage(image); |
| 8 | + image = scale2xImage(image); |
| 9 | + image = rotateAndReduceImage(image, angle); |
| 10 | + return image; |
| 11 | +} |
| 12 | + |
| 13 | +function scale2xImage(original) { |
| 14 | + let scaled = Pixels.create(original.width << 1, original.height << 1); |
| 15 | + for (let x = 0; x < original.width; x++) { |
| 16 | + for (let y = 0; y < original.height; y++) { |
| 17 | + const p = original.getPixel(x, y); |
| 18 | + const a = original.getPixel(x, y - 1); |
| 19 | + const b = original.getPixel(x + 1, y); |
| 20 | + const c = original.getPixel(x - 1, y); |
| 21 | + const d = original.getPixel(x, y + 1); |
| 22 | + if (c == a && c != d && a != b) { |
| 23 | + scaled.setPixel(x << 1, y << 1, a); |
| 24 | + } else { |
| 25 | + scaled.setPixel(x << 1, y << 1, p); |
| 26 | + } |
| 27 | + if (a == b && a != c && b != d) { |
| 28 | + scaled.setPixel((x << 1) + 1, y << 1, b); |
| 29 | + } else { |
| 30 | + scaled.setPixel((x << 1) + 1, y << 1, p); |
| 31 | + } |
| 32 | + if (d == c && d != b && c != a) { |
| 33 | + scaled.setPixel(x << 1, (y << 1) + 1, c); |
| 34 | + } else { |
| 35 | + scaled.setPixel(x << 1, (y << 1) + 1, p); |
| 36 | + } |
| 37 | + if (b == d && b != a && d != c) { |
| 38 | + scaled.setPixel((x << 1) + 1, (y << 1) + 1, d); |
| 39 | + } else { |
| 40 | + scaled.setPixel((x << 1) + 1, (y << 1) + 1, p); |
| 41 | + } |
| 42 | + } |
| 43 | + } |
| 44 | + return scaled; |
| 45 | +} |
| 46 | +function rotateAndReduceImage(original, angle) { |
| 47 | + let rotated = Pixels.create(original.width >> 3, original.height >> 3); |
| 48 | + |
| 49 | + const centerX = rotated.width >> 1; |
| 50 | + const centerY = rotated.height >> 1; |
| 51 | + |
| 52 | + for (let x = 0; x < rotated.width; x++) { |
| 53 | + for (let y = 0; y < rotated.height; y++) { |
| 54 | + let dir = Math.atan2(y - centerY, x - centerX); |
| 55 | + let mag = Math.sqrt((x - centerX) ** 2 + (y - centerY) ** 2) << 3; |
| 56 | + |
| 57 | + dir = dir - angle; |
| 58 | + |
| 59 | + let origX = Math.round((centerX << 3) + mag * Math.cos(dir)); |
| 60 | + let origY = Math.round((centerY << 3) + mag * Math.sin(dir)); |
| 61 | + |
| 62 | + if ( |
| 63 | + origX >= 0 && |
| 64 | + origX < original.width && |
| 65 | + origY >= 0 && |
| 66 | + origY < original.height |
| 67 | + ) { |
| 68 | + rotated.setPixel(x, y, original.getPixel(origX, origY)); |
| 69 | + } |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + return rotated; |
| 74 | +} |
| 75 | + |
| 76 | +export class Pixels { |
| 77 | + // The width and height of the image in pixels |
| 78 | + width; |
| 79 | + height; |
| 80 | + // A Uint8Array representing the colors of the pixels in the image |
| 81 | + pixels; |
| 82 | + |
| 83 | + // Constructor to create a new Image object with the given dimensions |
| 84 | + constructor(width, height, pixels) { |
| 85 | + this.width = width; |
| 86 | + this.height = height; |
| 87 | + this.pixels = pixels || new Uint32Array(width * height); |
| 88 | + } |
| 89 | + |
| 90 | + // Returns the color of the pixel at the given coordinates |
| 91 | + getPixel = (x, y) => { |
| 92 | + return this.pixels[y * this.width + x]; |
| 93 | + }; |
| 94 | + |
| 95 | + // Sets the color of the pixel at the given coordinates to the given color |
| 96 | + setPixel = (x, y, color) => { |
| 97 | + this.pixels[y * this.width + x] = color; |
| 98 | + }; |
| 99 | + |
| 100 | + // Creates and returns a new Image object with the given dimensions |
| 101 | + static create = (width, height, pixels) => { |
| 102 | + return new Pixels(width, height, pixels); |
| 103 | + }; |
| 104 | + |
| 105 | + // Returns a new Image object with the same dimensions and pixel colors as the original image |
| 106 | + clone = () => { |
| 107 | + const clonedImage = new Pixels(this.width, this.height); |
| 108 | + for (let y = 0; y < this.height; y++) { |
| 109 | + for (let x = 0; x < this.width; x++) { |
| 110 | + clonedImage.setPixel(x, y, this.getPixel(x, y)); |
| 111 | + } |
| 112 | + } |
| 113 | + return clonedImage; |
| 114 | + }; |
| 115 | +} |
0 commit comments