Author: bernhard
Date: Thu Oct 20 13:14:04 2005
New Revision: 9521
Added:
trunk/examples/pir/circle.pir
Modified:
trunk/MANIFEST
trunk/t/examples/pir.t
Log:
Added a circle drawing example in PIR.
Thanks to Nick Glencross [perl #37487]
Added a test for circle.pir.
Modified: trunk/MANIFEST
==============================================================================
--- trunk/MANIFEST (original)
+++ trunk/MANIFEST Thu Oct 20 13:14:04 2005
@@ -599,6 +599,7 @@ examples/pasm/99beer.pasm
examples/pasm/cat.pasm [main]doc
examples/pasm/fact.pasm [main]doc
examples/pasm/hello.pasm [main]doc
+examples/pir/circle.pir [main]doc
examples/pir/euclid.pir [main]doc
examples/pir/hanoi.pir [main]doc
examples/pir/mandel.pir [main]doc
Added: trunk/examples/pir/circle.pir
==============================================================================
--- (empty file)
+++ trunk/examples/pir/circle.pir Thu Oct 20 13:14:04 2005
@@ -0,0 +1,119 @@
+# Copyright (C) 2005 The Perl Foundation. All rights reserved.
+# $Id$
+
+=head1 NAME
+
+examples/pir/circle.pir - Draw a circle on any VT100-compatible screen
+
+=head1 SYNOPSIS
+
+ % ./parrot examples/pir/circle.pir
+
+=head1 DESCRIPTION
+
+This draws an ASCII-art representation of a circle (or oval) using only
+integer arithmetic and no trig.
+
+The algorithm is based on:
+
+ cos(theta+delta) =
+ cos(theta) -
+ [alpha*cos(theta) + beta*sin(theta)]
+
+and
+
+ sin(theta+delta) =
+ sin(theta) -
+ [alpha*sin(theta) - beta*cos(theta)]
+
+where
+
+ alpha = 2*sin^2(delta/2) and beta = sin(delta)
+
+(See Numerical Recipes in C / The Art of Scientific Computing)
+
+In this program the value of delta is chosen to be 1/(1<<n) such
+that it is small enough that
+
+=over 4
+
+=item * alpha is approximately 0
+
+=item * beta is approximately delta
+
+=back
+
+=head1 AUTHOR
+
+Written for parrot by Nick Glencross <[EMAIL PROTECTED]>. Adapted
+from a machine code program that I wrote for a BBC micro.
+
+=head1 COPYRIGHT
+
+Same as parrot.
+
+=cut
+
+.sub circle :main
+
+ # These are configurable depending on your screen size
+ .local int width, height
+ width = 80
+ height = 24
+
+ # Calculate screen scaling and offset constants
+ .local int offset_x, offset_y, scale_x, scale_y
+ offset_x = width / 2
+ offset_y = height / 2
+
+ scale_x = 140000 / width
+ scale_y = 140000 / height
+
+ # Clear the screen
+ print "\033[H\033[2J"
+
+ # Initialise values
+ .local int x, y, counter
+ x = 0
+ y = 65536
+ counter = 0
+
+LOOP:
+
+ # Calculate screen coordinates
+ .local int screen_x, screen_y
+ screen_x = x / scale_x
+ screen_x += offset_x
+
+ screen_y = y / scale_y
+ screen_y += offset_y
+
+ print "\033["
+ print screen_y
+ print ";"
+ print screen_x
+ print "H*"
+
+ # The magic to compute the next position
+ .local int temp1, temp2
+ temp1 = x >> 6
+ temp2 = y >> 6
+ x += temp2
+ y -= temp1
+
+ # Loop again
+ inc counter
+ if counter < 404 goto LOOP
+
+ # Place cursor back at bottom of screen
+ print "\033["
+ print height
+ print ";0H"
+ print "\n"
+
+ # Done!
+ end
+
+.end
+
+# vim: ft=imc sw=4:
Modified: trunk/t/examples/pir.t
==============================================================================
--- trunk/t/examples/pir.t (original)
+++ trunk/t/examples/pir.t Thu Oct 20 13:14:04 2005
@@ -32,7 +32,7 @@ Bernhard Schmalhofer - <Bernhard.Schmalh
=cut
use strict;
-use Parrot::Test tests => 4;
+use Parrot::Test tests => 5;
use Test::More;
use Parrot::Config;
@@ -40,6 +40,9 @@ my $PARROT = ".$PConfig{slash}$PConfig{t
# Set up expected output for examples
my %expected = (
+ 'circle.pir' => << 'END_EXPECTED',
+[H[2J[23;40H*[23;40H*[23;41H*[23;41H*[23;42H*[23;42H*[23;43H*[23;44H*[23;44H*[23;45H*[23;45H*[23;46H*[23;46H*[23;47H*[22;48H*[22;48H*[22;49H*[22;49H*[22;50H*[22;50H*[22;51H*[22;52H*[22;52H*[22;53H*[22;53H*[22;54H*[22;54H*[22;55H*[22;55H*[22;56H*[22;56H*[21;57H*[21;58H*[21;58H*[21;59H*[21;59H*[21;60H*[21;60H*[21;61H*[21;61H*[21;62H*[21;62H*[20;62H*[20;63H*[20;63H*[20;64H*[20;64H*[20;65H*[20;65H*[20;66H*[20;66H*[19;66H*[19;67H*[19;67H*[19;68H*[19;68H*[19;68H*[19;69H*[18;69H*[18;70H*[18;70H*[18;70H*[18;71H*[18;71H*[18;71H*[17;72H*[17;72H*[17;72H*[17;72H*[17;73H*[17;73H*[17;73H*[16;74H*[16;74H*[16;74H*[16;74H*[16;75H*[16;75H*[15;75H*[15;75H*[15;75H*[15;76H*[15;76H*[15;76H*[14;76H*[14;76H*[14;76H*[14;77H*[14;77H*[14;77H*[13;77H*[13;77H*[13;77H*[13;77H*[13;77H*[12;77H*[12;77H*[12;77H*[12;77H*[12;77H*[12;77H*[12;77H*[12;77H*[12;77H*[12;77H*[12;77H*[12;77H*[11;77H*[11;77H*[11;77H*[11;77H*[11;77H*[10;77H*[10;77H*[10;77H*[10;77H*[10;76H*[10;76H*[9;76H*[9;76H*[9;76H*[9;76H*[9;75H*[9;75H*[8;75H*[8;75H*[8;75H*[8;74H*[8;74H*[8;74H*[7;74H*[7;73H*[7;73H*[7;73H*[7;72H*[7;72H*[7;72H*[6;72H*[6;71H*[6;71H*[6;71H*[6;70H*[6;70H*[5;70H*[5;69H*[5;69H*[5;68H*[5;68H*[5;68H*[5;67H*[5;67H*[4;66H*[4;66H*[4;66H*[4;65H*[4;65H*[4;64H*[4;64H*[4;63H*[3;63H*[3;62H*[3;62H*[3;61H*[3;61H*[3;60H*[3;60H*[3;59H*[3;59H*[3;58H*[2;58H*[2;57H*[2;57H*[2;56H*[2;56H*[2;55H*[2;55H*[2;54H*[2;54H*[2;53H*[2;52H*[2;52H*[2;51H*[2;51H*[1;50H*[1;50H*[1;49H*[1;48H*[1;48H*[1;47H*[1;47H*[1;46H*[1;46H*[1;45H*[1;44H*[1;44H*[1;43H*[1;43H*[1;42H*[1;41H*[1;41H*[1;40H*[1;40H*[1;40H*[1;39H*[1;39H*[1;38H*[1;38H*[1;37H*[1;36H*[1;36H*[1;35H*[1;35H*[1;34H*[1;33H*[1;33H*[1;32H*[1;32H*[1;31H*[1;30H*[1;30H*[1;29H*[2;29H*[2;28H*[2;28H*[2;27H*[2;26H*[2;26H*[2;25H*[2;25H*[2;24H*[2;24H*[2;23H*[2;23H*[2;22H*[2;22H*[3;21H*[3;21H*[3;20H*[3;20H*[3;19H*[3;19H*[3;18H*[3;18H*[3;17H*[3;17H*[4;16H*[4;16H*[4;15H*[4;15H*[4;14H*[4;14H*[4;13H*[4;13H*[5;13H*[5;12H*[5;12H*[5;11H*[5;11H*[5;11H*[5;10H*[5;10H*[6;9H*[6;9H*[6;9H*[6;8H*[6;8H*[6;8H*[7;7H*[7;7H*[7;7H*[7;6H*[7;6H*[7;6H*[7;6H*[8;5H*[8;5H*[8;5H*[8;5H*[8;4H*[8;4H*[9;4H*[9;4H*[9;4H*[9;3H*[9;3H*[10;3H*[10;3H*[10;3H*[10;3H*[10;2H*[10;2H*[11;2H*[11;2H*[11;2H*[11;2H*[11;2H*[11;2H*[12;2H*[12;2H*[12;2H*[12;2H*[12;2H*[12;2H*[12;2H*[12;2H*[12;2H*[12;2H*[12;2H*[13;2H*[13;2H*[13;2H*[13;2H*[13;2H*[14;2H*[14;2H*[14;2H*[14;3H*[14;3H*[14;3H*[15;3H*[15;3H*[15;3H*[15;4H*[15;4H*[16;4H*[16;4H*[16;4H*[16;5H*[16;5H*[16;5H*[17;5H*[17;6H*[17;6H*[17;6H*[17;6H*[17;7H*[17;7H*[18;7H*[18;8H*[18;8H*[18;8H*[18;9H*[18;9H*[19;9H*[19;10H*[19;10H*[19;10H*[19;11H*[19;11H*[19;12H*[20;12H*[20;12H*[20;13H*[20;13H*[20;14H*[20;14H*[20;15H*[20;15H*[21;16H*[21;16H*[21;17H*[21;17H*[21;18H*[21;18H*[21;19H*[21;19H*[21;20H*[22;20H*[22;21H*[22;21H*[22;22H*[22;22H*[22;23H*[22;23H*[22;24H*[22;24H*[22;25H*[22;25H*[22;26H*[23;27H*[23;27H*[23;28H*[23;28H*[23;29H*[23;30H*[23;30H*[23;31H*[23;31H*[23;32H*[23;32H*[23;33H*[23;34H*[23;34H*[23;35H*[23;36H*[23;36H*[23;37H*[23;37H*[23;38H*[23;39H*[23;39H*[23;40H*[23;40H*[23;40H*[24;0H
+END_EXPECTED
'euclid.pir' => << 'END_EXPECTED',
Algorithm E (Euclid's algorithm)
The greatest common denominator of 96 and 64 is 32.