Dragon curve: Difference between revisions

Content added Content deleted
imported>Thebeez
(Added uBasic/4tH version)
Line 1,153: Line 1,153:


Valid coordinates on the TI-89's graph screen are x 0..76 and y 0..158. This and [[wp:File:Dimensions_fractale_dragon.gif|the outer size of the dragon curve]] were used to choose the position and scale determined by the [[wp:Transformation_matrix#Affine_transformations|transformation matrix]] initially passed to <code>dragon</code> such that the curve will fit onscreen no matter the number of recursions chosen. The height of the curve is 1 unit, so the vertical (and horizontal, to preserve proportions) scale is the height of the screen (rather, one less, to avoid rounding/FP error overrunning), or 75. The curve extends 1/3 unit above its origin, so the vertical translation is (one more than) 1/3 of the scale, or 26. The curve extends 1/3 to the left of its origin, or 25 pixels; the width of the curve is 1.5 units, or 1.5·76 = 114 pixels, and the screen is 159 pixels, so to center it we place the origin at 25 + (159-114)/2 = 47 pixels.
Valid coordinates on the TI-89's graph screen are x 0..76 and y 0..158. This and [[wp:File:Dimensions_fractale_dragon.gif|the outer size of the dragon curve]] were used to choose the position and scale determined by the [[wp:Transformation_matrix#Affine_transformations|transformation matrix]] initially passed to <code>dragon</code> such that the curve will fit onscreen no matter the number of recursions chosen. The height of the curve is 1 unit, so the vertical (and horizontal, to preserve proportions) scale is the height of the screen (rather, one less, to avoid rounding/FP error overrunning), or 75. The curve extends 1/3 unit above its origin, so the vertical translation is (one more than) 1/3 of the scale, or 26. The curve extends 1/3 to the left of its origin, or 25 pixels; the width of the curve is 1.5 units, or 1.5·76 = 114 pixels, and the screen is 159 pixels, so to center it we place the origin at 25 + (159-114)/2 = 47 pixels.

=={{header|uBasic/4tH}}==
{{Trans|BBC BASIC}}
uBasic/4tH has neither native support for graphics nor floating point, so everything has to be defined in high level code. All calculations are done in integer arithmetic, scaled by 10K.
<syntaxhighlight lang="ubasic-4th">
Dim @o(5) ' 0 = SVG file, 1 = color, 2 = fillcolor, 3 = pixel, 4 = text

' === Begin Program ===

Proc _SetColor (FUNC(_Color ("Red"))) ' set the line color to red
Proc _SVGopen ("dragon.svg") ' open the SVG file
Proc _Canvas (525, 625) ' set the canvas size
Proc _Background (FUNC(_Color ("White")))
' we have a white background
a = 475 : b = 175 : t = 14142 : r = 0 : p = 7853
' x,y coordinates, SQRT(2), angle, PI/4
Proc _Dragon (512, 12, 1) ' size, split and direction
Proc _SVGclose ' close SVG file
End

_Dragon
Param (3)
If b@ Then ' if split > 0 then recurse
r = r + (c@ * p)
Proc _Dragon ((a@*10000)/t, b@ - 1, 1)
r = r - (c@ * (p+p))
Proc _Dragon ((a@*10000)/t, b@ - 1, -1)
r = r + (c@ * p)
Return
EndIf
' draw a line
Proc _Line (a, b, Set (a, a + (((-1 * FUNC(_COS(r)))*a@)/10000)), Set (b, b + ((FUNC(_SIN(r))*a@)/10000)))
Return

' === End Program ===

_SetColor Param (1) : @o(1) = a@ : Return
_SVGclose Write @o(0), "</svg>" : Close @o(0) : Return
_color_ Param (1) : Proc _PrintRGB (a@) : Write @o(0), "\q />" : Return

_PrintRGB ' print an RBG color in hex
Param (1)
Radix 16

If a@ < 0 Then
Write @o(0), "none";
Else
Write @o(0), Show(Str ("#!######", a@));
EndIf

Radix 10
Return

_Background ' set the background color
Param (1)

Write @o(0), "<rect width=\q100%\q height=\q100%\q fill=\q";
Proc _color_ (a@)
Return

_Color ' retrieve color code from its name
Param (1)
Local (1)
Radix 16

if Comp(a@, "black") = 0 Then
b@ = 000000
else if Comp(a@, "blue") = 0 Then
b@ = 0000ff
else if Comp(a@, "green") = 0 Then
b@ = 00ff00
else if Comp(a@, "cyan") = 0 Then
b@ = 00ffff
else if Comp(a@, "red") = 0 Then
b@ = 0ff0000
else if Comp(a@, "magenta") = 0 Then
b@ = 0ff00ff
else if Comp(a@, "yellow") = 0 Then
b@ = 0ffff00
else if Comp(a@, "white") = 0 Then
b@ = 0ffffff
else if Comp(a@, "none") = 0 Then
b@ = Info ("nil")
else Print "Invalid color" : Raise 1
fi : fi : fi : fi : fi : fi : fi : fi : fi

Radix 10
Return (b@)

_Line ' draw an SVG line from x1,y1 to x2,y2
Param (4)

Write @o(0), "<line x1=\q";d@;"\q y1=\q";c@;
Write @o(0), "\q x2=\q";b@;"\q y2=\q";a@;"\q stroke=\q";
Proc _color_ (@o(1))
Return

_Canvas ' set up a canvas x wide and y high
Param (2)
Write @o(0), "<svg width=\q";a@;"\q height=\q";b@;"\q viewBox=\q0 0 ";a@;" ";b@;
Write @o(0), "\q xmlns=\qhttp://www.w3.org/2000/svg\q ";
Write @o(0), "xmlns:xlink=\qhttp://www.w3.org/1999/xlink\q>"
Return

_SVGopen ' open an SVG file by name
Param (1)

If Set (@o(0), Open (a@, "w")) < 0 Then
Print "Cannot open \q";Show (a@);"\q" : Raise 1
Else
Write @o(0), "<?xml version=\q1.0\q encoding=\qUTF-8\q standalone=\qno\q?>"
Write @o(0), "<!DOCTYPE svg PUBLIC \q-//W3C//DTD SVG 1.1//EN\q ";
Write @o(0), "\qhttp://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\q>"
EndIf
Return
' return SIN(x*10K), scaled by 10K
_SIN PARAM(1) : PUSH A@ : LET A@=TOS()<0 : PUSH ABS(POP()%62832)
IF TOS()>31416 THEN A@=A@=0 : PUSH POP()-31416
IF TOS()>15708 THEN PUSH 31416-POP()
PUSH (TOS()*TOS())/10000 : PUSH 10000+((10000*-(TOS()/72))/10000)
PUSH 10000+((POP()*-(TOS()/42))/10000) : PUSH 10000+((POP()*-(TOS()/20))/10000)
PUSH 10000+((POP()*-(POP()/6))/10000) : PUSH (POP()*POP())/10000
IF A@ THEN PUSH -POP()
RETURN
' return COS(x*10K), scaled by 10K
_COS PARAM(1) : PUSH ABS(A@%62832) : IF TOS()>31416 THEN PUSH 62832-POP()
LET A@=TOS()>15708 : IF A@ THEN PUSH 31416-POP()
PUSH TOS() : PUSH (POP()*POP())/10000 : PUSH 10000+((10000*-(TOS()/56))/10000)
PUSH 10000+((POP()*-(TOS()/30))/10000): PUSH 10000+((POP()*-(TOS()/12))/10000)
PUSH 10000+((POP()*-(POP()/2))/10000) : IF A@ THEN PUSH -POP()
RETURN
</syntaxhighlight>


==={{header|VBScript}}===
==={{header|VBScript}}===