Jump to content

Dragon curve: Difference between revisions

(Dragon curve en FreeBASIC)
Line 3,664:
 
<div style="text-align:center">[[Image:Dragon1.png]]</div>
 
=={{header|Nim}}==
{{trans|Go}}
{{libheader|nimPNG}}
The program is an adaptation of the second version of Go solution with some changes.
 
As in the Go solution, we use the module created for the Bitmap task. To draw the lines, we use the module created for the task “bitmap Bresenham’s line algorithm”. But, rather than producing a big PPM file, we output a PNG file by using the third party module “nimPNG”.
 
<lang Nim>import math
import bitmap, bresenham, nimPNG
 
const
## Separation of the two endpoints.
## Make this a power of 2 for prettier output.
Sep = 512
## Depth of recursion. Adjust as desired for different visual effects.
Depth = 18
 
S = sqrt(2.0) / 2
Sin = [float 0, S, 1, S, 0, -S, -1, -S]
Cos = [float 1, S, 0, -S, -1, -S, 0, S]
 
LineColor = (64u8, 192u8, 96u8)
Width = Sep * 11 div 6
Height = Sep * 4 div 3
 
Output = "dragon.png"
 
#---------------------------------------------------------------------------------------------------
 
func dragon(img: Image; n, a, t: int; d, x, y: float) =
if n <= 1:
img.drawLine((x.toInt, y.toInt), ((x + d * Cos[a]).toInt, (y + d * Sin[a]).toInt), LineColor)
return
let d = d * S
let a1 = (a - t) and 7
let a2 = (a + t) and 7
img.dragon(n - 1, a1, 1, d, x, y)
img.dragon(n - 1, a2, -1, d, x + d * Cos[a1], y + d * Sin[a1])
 
#---------------------------------------------------------------------------------------------------
 
var image = newImage(Width, Height)
image.fill((0u8, 0u8, 0u8))
image.dragon(Depth, 0, 1, Sep, Sep / 2, Sep * 5 / 6)
 
# Prepare data to save it as a PNG image.
var data = newSeqOfCap[byte](image.pixels.len * 3)
for color in image.pixels:
data.add([color.r, color.g, color.b])
 
let status = savePNG24(Output, data, image.w, image.h)
if status.isOk:
echo "Result is available in file ", Output
else:
echo "Error: ", status.error</lang>
 
=={{header|OCaml}}==
Anonymous user
Cookies help us deliver our services. By using our services, you agree to our use of cookies.