1
0
Fork 0
mirror of https://github.com/Orama-Interactive/Pixelorama.git synced 2025-01-18 17:19:50 +00:00

Some code improvements in DrawingAlgos

Should also result in minor optimizations
This commit is contained in:
Emmanouil Papadeas 2024-01-31 02:26:49 +02:00
parent 399fc8dfbd
commit e648d0d612

View file

@ -156,36 +156,22 @@ func scale_3x(sprite: Image, tol := 50.0) -> Image:
var scaled := Image.create( var scaled := Image.create(
sprite.get_width() * 3, sprite.get_height() * 3, false, Image.FORMAT_RGBA8 sprite.get_width() * 3, sprite.get_height() * 3, false, Image.FORMAT_RGBA8
) )
var a: Color var width_minus_one := sprite.get_width() - 1
var b: Color var height_minus_one := sprite.get_height() - 1
var c: Color
var d: Color
var e: Color
var f: Color
var g: Color
var h: Color
var i: Color
for x in range(0, sprite.get_width()): for x in range(0, sprite.get_width()):
for y in range(0, sprite.get_height()): for y in range(0, sprite.get_height()):
var xs := 3 * x var xs := 3 * x
var ys := 3 * y var ys := 3 * y
a = sprite.get_pixel(maxi(x - 1, 0), maxi(y - 1, 0)) var a := sprite.get_pixel(maxi(x - 1, 0), maxi(y - 1, 0))
b = sprite.get_pixel(mini(x, sprite.get_width() - 1), maxi(y - 1, 0)) var b := sprite.get_pixel(mini(x, width_minus_one), maxi(y - 1, 0))
c = sprite.get_pixel(mini(x + 1, sprite.get_width() - 1), maxi(y - 1, 0)) var c := sprite.get_pixel(mini(x + 1, width_minus_one), maxi(y - 1, 0))
d = sprite.get_pixel(maxi(x - 1, 0), mini(y, sprite.get_height() - 1)) var d := sprite.get_pixel(maxi(x - 1, 0), mini(y, height_minus_one))
e = sprite.get_pixel(mini(x, sprite.get_width() - 1), mini(y, sprite.get_height() - 1)) var e := sprite.get_pixel(mini(x, width_minus_one), mini(y, height_minus_one))
f = sprite.get_pixel( var f := sprite.get_pixel(mini(x + 1, width_minus_one), mini(y, height_minus_one))
mini(x + 1, sprite.get_width() - 1), mini(y, sprite.get_height() - 1) var g := sprite.get_pixel(maxi(x - 1, 0), mini(y + 1, height_minus_one))
) var h := sprite.get_pixel(mini(x, width_minus_one), mini(y + 1, height_minus_one))
g = sprite.get_pixel(maxi(x - 1, 0), mini(y + 1, sprite.get_height() - 1)) var i := sprite.get_pixel(mini(x + 1, width_minus_one), mini(y + 1, height_minus_one))
h = sprite.get_pixel(
mini(x, sprite.get_width() - 1), mini(y + 1, sprite.get_height() - 1)
)
i = sprite.get_pixel(
mini(x + 1, sprite.get_width() - 1), mini(y + 1, sprite.get_height() - 1)
)
var db: bool = similar_colors(d, b, tol) var db: bool = similar_colors(d, b, tol)
var dh: bool = similar_colors(d, h, tol) var dh: bool = similar_colors(d, h, tol)
@ -223,7 +209,7 @@ func scale_3x(sprite: Image, tol := 50.0) -> Image:
func rotxel(sprite: Image, angle: float, pivot: Vector2) -> void: func rotxel(sprite: Image, angle: float, pivot: Vector2) -> void:
# If angle is simple, then nn rotation is the best # If angle is simple, then nn rotation is the best
if angle == 0 || angle == PI / 2 || angle == PI || angle == 2 * PI: if angle == 0 || angle == PI / 2 || angle == PI || angle == 3.0 * PI / 2.0 || angle == TAU:
nn_rotate(sprite, angle, pivot) nn_rotate(sprite, angle, pivot)
return return
@ -231,7 +217,6 @@ func rotxel(sprite: Image, angle: float, pivot: Vector2) -> void:
aux.copy_from(sprite) aux.copy_from(sprite)
var ox: int var ox: int
var oy: int var oy: int
var p: Color
for x in sprite.get_size().x: for x in sprite.get_size().x:
for y in sprite.get_size().y: for y in sprite.get_size().y:
var dx := 3 * (x - pivot.x) var dx := 3 * (x - pivot.x)
@ -269,27 +254,19 @@ func rotxel(sprite: Image, angle: float, pivot: Vector2) -> void:
ox = roundi((ox - 1) / 3.0) ox = roundi((ox - 1) / 3.0)
oy = roundi((oy - 1) / 3.0) oy = roundi((oy - 1) / 3.0)
var a: Color var p: Color
var b: Color
var c: Color
var d: Color
var e: Color
var f: Color
var g: Color
var h: Color
var i: Color
if ox == 0 || ox == sprite.get_width() - 1 || oy == 0 || oy == sprite.get_height() - 1: if ox == 0 || ox == sprite.get_width() - 1 || oy == 0 || oy == sprite.get_height() - 1:
p = aux.get_pixel(ox, oy) p = aux.get_pixel(ox, oy)
else: else:
a = aux.get_pixel(ox - 1, oy - 1) var a := aux.get_pixel(ox - 1, oy - 1)
b = aux.get_pixel(ox, oy - 1) var b := aux.get_pixel(ox, oy - 1)
c = aux.get_pixel(ox + 1, oy - 1) var c := aux.get_pixel(ox + 1, oy - 1)
d = aux.get_pixel(ox - 1, oy) var d := aux.get_pixel(ox - 1, oy)
e = aux.get_pixel(ox, oy) var e := aux.get_pixel(ox, oy)
f = aux.get_pixel(ox + 1, oy) var f := aux.get_pixel(ox + 1, oy)
g = aux.get_pixel(ox - 1, oy + 1) var g := aux.get_pixel(ox - 1, oy + 1)
h = aux.get_pixel(ox, oy + 1) var h := aux.get_pixel(ox, oy + 1)
i = aux.get_pixel(ox + 1, oy + 1) var i := aux.get_pixel(ox + 1, oy + 1)
match index: match index:
0: 0:
@ -414,9 +391,7 @@ func rotxel(sprite: Image, angle: float, pivot: Vector2) -> void:
func fake_rotsprite(sprite: Image, angle: float, pivot: Vector2) -> void: func fake_rotsprite(sprite: Image, angle: float, pivot: Vector2) -> void:
var selected_sprite := Image.new() var selected_sprite := scale_3x(sprite)
selected_sprite.copy_from(sprite)
selected_sprite.copy_from(scale_3x(selected_sprite))
nn_rotate(selected_sprite, angle, pivot * 3) nn_rotate(selected_sprite, angle, pivot * 3)
selected_sprite.resize( selected_sprite.resize(
selected_sprite.get_width() / 3, selected_sprite.get_height() / 3, Image.INTERPOLATE_NEAREST selected_sprite.get_width() / 3, selected_sprite.get_height() / 3, Image.INTERPOLATE_NEAREST
@ -429,12 +404,12 @@ func nn_rotate(sprite: Image, angle: float, pivot: Vector2) -> void:
return return
var aux := Image.new() var aux := Image.new()
aux.copy_from(sprite) aux.copy_from(sprite)
var ox: int var angle_sin := sin(angle)
var oy: int var angle_cos := cos(angle)
for x in range(sprite.get_width()): for x in range(sprite.get_width()):
for y in range(sprite.get_height()): for y in range(sprite.get_height()):
ox = (x - pivot.x) * cos(angle) + (y - pivot.y) * sin(angle) + pivot.x var ox := (x - pivot.x) * angle_cos + (y - pivot.y) * angle_sin + pivot.x
oy = -(x - pivot.x) * sin(angle) + (y - pivot.y) * cos(angle) + pivot.y var oy := -(x - pivot.x) * angle_sin + (y - pivot.y) * angle_cos + pivot.y
if ox >= 0 && ox < sprite.get_width() && oy >= 0 && oy < sprite.get_height(): if ox >= 0 && ox < sprite.get_width() && oy >= 0 && oy < sprite.get_height():
sprite.set_pixel(x, y, aux.get_pixel(ox, oy)) sprite.set_pixel(x, y, aux.get_pixel(ox, oy))
else: else: