Categories
Posts in this category
- A shiny perl6.org site
- Creating an entry point for newcomers
- Sprixel, a 6 compiler powered by JavaScript
- Another perl6.org iteration
- Blackjack and Perl 6
- Why I commit Crud to the Perl 6 Test Suite
- Report from the Perl 6 Hackathon in Copenhagen
- Custom operators in Rakudo
- Defined Behaviour with Undefined Values
- Dissecting the "Starry obfu"
- Perl 6: Failing Softly with Unthrown Exceptions
- The first Perl 6 module on CPAN
- Google Summer of Code Mentor Recap
- Building a Huffman Tree With Rakudo
- Immutable Sigils and Context
- Is Perl 6 really Perl?
- Perl 6: Lost in Wonderland
- Lots of momentum in the Perl 6 community
- Musing and the future of feather and the Pugs repository
- Musings on Rakudo's spectest chart
- My first executable from Perl 6
- Trying to implement new operators - failed
- Let's build an object
- Perl 6 is optimized for fun
- How to get a parse tree for a Perl 6 Program
- Perl 6 in 2009
- Perl 6 ticket life cycle
- The Perl 6 Advent Calendar
- How to Plot a Segment of a Circle with SVG
- Publicity for Perl 6
- Rakudo architectural overview
- Rakudo Rocks
- Rakudo "star" announced
- Rakudo's rough edges
- Rats and other pets
- Releasing Rakudo made easy
- Set Phasers to Stun!
- Starry Perl 6 obfu
- Recent Perl 6 Developments August 2008
- Strings and Buffers
- Subroutines vs. Methods - Differences and Commonalities
- A SVG plotting adventure
- A Syntax Highlighter for Perl 6
- Test Suite Reorganization: How to move tests
- The Happiness of Design Convergence
- Perl 6 Tidings from September and October 2008
- Perl 6 Tidings for November 2008
- Perl 6 Tidings from December 2008
- Perl 6 Tidings from January 2009
- Perl 6 Tidings from February 2009
- Perl 6 Tidings from March 2009
- Perl 6 Tidings from April 2009
- Perl 6 Tidings from May 2009
- Perl 6 Tidings from May 2009 (second iteration)
- Perl 6 Tidings from June 2009
- Perl 6 Tidings from August 2009
- Perl 6 Tidings from October 2009
- Timeline for a syntax change in Perl 6
- Visualizing match trees
- We write a Perl 6 book for you
- When we reach 100% we did something wrong
- Where Rakudo Lives Now
- Why was the Perl 6 Advent Calendar such a Success?
- What you can write in Perl 6 today
- Why you don't need the Y combinator in Perl 6
Mon, 07 Sep 2009
How to Plot a Segment of a Circle with SVG
Permanent link
I wanted to generate some pie charts with SVG (for SVG::Plot), and a search on the Internet quickly showed that I need paths for that. But the exact usage remained unclear to me until I read the specs and did some experimenting.
The way to go is:
- Move to (M) the center of the circle
- Line to (l) the starting point of the arc
- Arc to (A) the end point of the arc
- Close the path (z)
The start and end point of the arc can be calculated as
x = center_x + radius * cos(angle) y = center_y + radius * sin(angle)
The parameters to the A command in the path are rx, ry,
axis-rotation large-arc-flag sweep-flag x y.
For our purposes rx and ry need to be just the radius of the circle, large-arc-flag is 1 if the difference between start angle and end angle is larger than pi (or 180°). sweep-flag is 1 if we assume that the start angle is smaller than the end angle (and thus, since the SVG coordinate system has the positive y axis downwards, plot clockwise). x and y are the coordinates of the end point.
The code I used to generate the SVG above is (in Perl 6, using Carl Mäsak's SVG module):
use v6; BEGIN { @*INC.push: 'src/svg/lib' } use SVG; sub arc($cx = 100, $cy = 100, $r = 50, :$start, :end($phi), :$color = 'red') { my @commands = 'M', $cx, $cy, 'l', $r * cos($start), $r * sin($start), 'A', $r, $r, 0, + ($phi - $start > pi), 1, $cx + $r * cos($phi), $cy + $r * sin($phi), "z"; my $c = join ' ', @commands; return 'g' => [ :stroke<none>, :fill($color), path => [ :d($c) ], ]; } say SVG.serialize( 'svg' => [ :width(200), :height(200), 'xmlns' => 'http://www.w3.org/2000/svg', 'xmlns:svg' => 'http://www.w3.org/2000/svg', 'xmlns:xlink' => 'http://www.w3.org/1999/xlink', arc(:color<blue>, :start(0), :end(pi/3)), arc(:color<red>, :start(pi/3), :end(3 * pi / 2)), arc(:color<yellow>, :start(3 * pi / 2), :end(0)), ]);
The only syntactic feature here worth explaining is that
+ ( $thing > pi ) returns 1 if $thing is larger
than pi and 0 otherwise. Everything else should be straight
forward.