Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked

optical encoder

by oluwaseun
on Mar 16, 2012 at 02:52 UTC ( #959903=perlquestion: print w/replies, xml ) Need Help??
oluwaseun has asked for the wisdom of the Perl Monks concerning the following question:

Please help me with the code to generate a circle of 180 divisions and a diameter of 15cm.It's for a projet I never prepared for.

Replies are listed 'Best First'.
Re: optical encoder
by moritz (Cardinal) on Mar 16, 2012 at 08:59 UTC
    Something like this should work (though I haven't tested it) with the current Rakudo Star release:
    use v6; use SVG; my $r = 150 / 2; my @parts = :circle[ cx => $r, cy => $r, r => $r, stlye => 'fill:none; stroke: black; stroke-width: 1', ]; for 0, 2 ...^ 360 -> $degrees { my $rad = $degrees / 360 * 2 * pi; @parts.push: 'line' => [ x1 => $r, y1 => $r, x2 => $r + $r * cos($rad), y2 => $r + $r * sin($rad), ]; } say SVG.serialize: 'svg' => [ width => 2 * $r, height => 2 * $r, @parts, ];

    Run it as

    perl6 > circle.svg

    and scale to your desired size.

      The SVG module seems to misspell the style attribute as "stlye".

      From the output:

      <svg xmlns="" xmlns:svg=" +000/svg" xmlns:xlink="" width="150 " height="150"><circle cx="75" cy="75" r="75" stlye="fill:none; stroke +: black; stroke-width: 1" />

      Visio 2007 also doesn't recognize the xmlns:svg attribute, but I'm going to assume that is Visio's fault.

        The misspelling is right in the code I've posted, not in the module. In fact doesn't handle style specially in any way (though I've pondered it should).

        I've now actually tested the code, and this seems to work (the resulting SVG displays fine in firefox and inkscape).

        use v6; use SVG; my $r = 150 / 2; my @parts = :circle[ cx => $r, cy => $r, r => $r, style => 'fill:none; stroke: black; stroke-width: 1', ]; for 0, 2 ...^ 360 -> $degrees { my $rad = $degrees / 360 * 2 * pi; @parts.push: 'line' => [ x1 => $r, y1 => $r, x2 => $r + $r * cos($rad), y2 => $r + $r * sin($rad), style => 'stroke: black; stroke-width: 1', ]; } say SVG.serialize: 'svg' => [ width => 2 * $r, height => 2 * $r, @parts, ];

        As for the xmlns:svg declaration, that just means that the svg XML namespace is set up as being interpreted as SVG. This allows you (in theory) to do fun stuff like embedding <svg:rect ...> elements straight into XHTML. But even for browsers that support it, it is only supported if the document is delivered as application/xml+xhtml, which is generally no fun, because most IE versions don't render such documents, but offer them for download. </rant>

Re: optical encoder
by davido (Archbishop) on Mar 16, 2012 at 03:36 UTC

    I don't understand. Is this a math question?

    15 * PI = 47.12 (approximately) 47.12 / 180 = 0.26 (approximately)

    So one division for every 2.6mm will get you 180 divisions on a circle with a diameter of 15cm.

    If you have a Perl question please feel free to post specifics on what part of the code you're having difficulty with.


Re: optical encoder
by DrHyde (Prior) on Mar 19, 2012 at 10:50 UTC
    What have you tried so far, and how do the results differ both from what you expected and what you wanted? Without that, we can't help you.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://959903]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (5)
As of 2018-01-21 13:48 GMT
Find Nodes?
    Voting Booth?
    How did you see in the new year?

    Results (228 votes). Check out past polls.