Bitmap/Bézier curves/Cubic: Difference between revisions
Content added Content deleted
m (→{{header|Wren}}: Changed to Wren S/H) |
imported>Chinhouse No edit summary |
||
Line 1,416: | Line 1,416: | ||
>> disp(img) |
>> disp(img) |
||
</syntaxhighlight> |
</syntaxhighlight> |
||
=={{header|MiniScript}}== |
|||
This GUI implementation is for use with [http://miniscript.org/MiniMicro Mini Micro]. |
|||
<syntaxhighlight lang="miniscript"> |
|||
Point = {"x": 0, "y":0} |
|||
Point.init = function(x, y) |
|||
p = new Point |
|||
p.x = x; p.y = y |
|||
return p |
|||
end function |
|||
drawLine = function(img, x0, y0, x1, y1, colr) |
|||
sign = function(a, b) |
|||
if a < b then return 1 |
|||
return -1 |
|||
end function |
|||
dx = abs(x1 - x0) |
|||
sx = sign(x0, x1) |
|||
dy = abs(y1 - y0) |
|||
sy = sign(y0, y1) |
|||
if dx > dy then |
|||
err = dx |
|||
else |
|||
err = -dy |
|||
end if |
|||
err = floor(err / 2) |
|||
while true |
|||
img.setPixel x0, y0, colr |
|||
if x0 == x1 and y0 == y1 then break |
|||
e2 = err |
|||
if e2 > -dx then |
|||
err -= dy |
|||
x0 += sx |
|||
end if |
|||
if e2 < dy then |
|||
err += dx |
|||
y0 += sy |
|||
end if |
|||
end while |
|||
end function |
|||
cubicBezier = function(img, p1, p2, p3, p4, numPoints, colr) |
|||
points = [] |
|||
for i in range(0, numPoints) |
|||
t = i / numPoints |
|||
u = 1 - t |
|||
a = (1 - t)^3 |
|||
b = 3 * t * (1 - t)^2 |
|||
c = 3 * t^2 * (1 - t) |
|||
d = t^3 |
|||
x = floor(a * p1.x + b * p2.x + c * p3.x + d * p4.x) |
|||
y = floor(a * p1.y + b * p2.y + c * p3.y + d * p4.y) |
|||
points.push(Point.init(x, y)) |
|||
img.setPixel x, y, colr |
|||
end for |
|||
for i in range(1, numPoints) |
|||
drawLine img, points[i-1].x, points[i-1].y, points[i].x, points[i].y, colr |
|||
end for |
|||
end function |
|||
bezier = Image.create(480, 480) |
|||
p1 = Point.init(50, 100) |
|||
p2 = Point.init(200, 400) |
|||
p3 = Point.init(360, 0) |
|||
p4 = Point.init(300, 424) |
|||
cubicBezier bezier, p1, p2, p3, p4, 20, color.red |
|||
gfx.clear |
|||
gfx.drawImage bezier, 0, 0 |
|||
</syntaxhighlight> |
|||
=={{header|Nim}}== |
=={{header|Nim}}== |
||
{{Trans|Ada}} |
{{Trans|Ada}} |