plotting/displaying regular polygons
plotting/displaying regular polygons
I want to write a math aplet for high school students that will display various regular polygons and let them see and change parameters to see how areas and perimeters vary depending upon the number of sides. Seems there should be an easy way to do this in apl. I am using Dyalog APL. Could be with or without RainPro, but I would like this to be easy so students can understand how this works(and me too). I think RainPro will migrate to web application better so would prefer that.
-
- Posts: 439
- Joined: Wed Oct 01, 2008 9:39 am
Re: plotting/displaying regular polygons
Hi Jerry,
Have a look at the Poly object in the Object Reference. I think this object will do what you want.
Regards,
Vince
Have a look at the Poly object in the Object Reference. I think this object will do what you want.
Regards,
Vince
Re: plotting/displaying regular polygons
Poly object is very simple. thanks Vince.
I have been playing with RainPro and have that sortta working, not quite as simple as Poly object but fairly clear.
Still haven't quite got how to calculate vertices of all polygons working but am close.
Is Poly object supported by miserver like RainPro is?
How can I cut and paste from object reference manual into my apl session to test things out easily?
I have been playing with RainPro and have that sortta working, not quite as simple as Poly object but fairly clear.
Still haven't quite got how to calculate vertices of all polygons working but am close.
Is Poly object supported by miserver like RainPro is?
How can I cut and paste from object reference manual into my apl session to test things out easily?
-
- Posts: 439
- Joined: Wed Oct 01, 2008 9:39 am
Re: plotting/displaying regular polygons
Hi Jerry,
You can draw the poly on a bitmap and then write the bitmap to a file.
The following code uses a form to show the bitmap, but you don't need a form.
You can copy from the Help->Gui Help or from the Object Reference pdf file. Highlight some text and press Ctrl-c or right click on the selection and choose copy.
Regards,
Vince
You can draw the poly on a bitmap and then write the bitmap to a file.
The following code uses a form to show the bitmap, but you don't need a form.
Code: Select all
poly1;Y;X
'f'⎕WC'form'('picture' 'b')
'b'⎕WC'bitmap'('bits'(256 256⍴16))('cmap'(16 3⍴255 255 255))
Y←10 10 50 50 10
X←10 50 50 10 10
'b.p'⎕WC'Poly'(Y X)(0 255 0)('fillcol' 0 255 0)('fstyle' 0)
'b' ⎕ws 'file' 'c:\temp\greenrect.bmp'
b.FileWrite
You can copy from the Help->Gui Help or from the Object Reference pdf file. Highlight some text and press Ctrl-c or right click on the selection and choose copy.
Regards,
Vince
Re: plotting/displaying regular polygons
jbrennan wrote:Still haven't quite got how to calculate vertices of all polygons working but am close.
In Dyalog version 13 you can generate the vertices of a regular n-sided polygon as points in the complex plane like this:
Code: Select all
{¯1*(2×⍳⍵)÷⍵} 7 ⍝ heptagon
0.6234898019J0.7818314825 ¯0.222520934J0.9749279122 ¯0.9009688679J0.4338837391
¯0.9009688679J¯0.4338837391 ¯0.222520934J¯0.9749279122
0.6234898019J¯0.7818314825 1
Re: plotting/displaying regular polygons
Got rainpro almost working polygon is just a little crooked. will try poly a bit more with your help. thanks for all your help
Here is what I have for rainpro. I am keeping xy axes in until I get it really straight then I will probably drop them.
R←PolyPlot(n s);y;x;y;foot;range;x0;y0;Deg2Rad;theta;i;radius;py;px;pct;area;apothem
⍝ n is number of sides s=side length. So: PolyPlot 5 10 would plot a 5 sided polygon with each side=10
Deg2Rad←{⍵×○1÷180} ⍝ fns to convert degrees to radians for input to trigonometric fns
radius←s÷2×1○Deg2Rad 180÷n ⍝ center to a vertex 1○ is sine
apothem←s÷2×3○Deg2Rad 180÷n ⍝ center to midpt side 3○ is tangent
area←(n×s*2)÷4×3○Deg2Rad 180÷n ⍝ area of polygon 3○ is tangent
x0←y0←0 ⍝ x y location of center of polygon on plot
⍝ see http://www.mathopenref.com/polygonregulararea.html for explanation of following formulas
theta←(360÷n)×i←0,(⍳n-1),0 ⍝ theta is angle with the x axis for plot based on # of sides (n)
x←x0+radius×2○Deg2Rad theta+i×(2×○1)÷n ⍝ x vertice locations 2○ is cosine
y←y0+radius×1○Deg2Rad theta+i×(2×○1)÷n ⍝ y vertice locations 1○ is sine
ch.New 350 350 ⍝ trying to make x y lengths the same but failing
⍝ ch.Set'syle' 'noaxes' must shut off other stuff to make this work
ch.Set'Head'((⍕n),' Sided Polygon Plot side length=',⍕s)
ch.Set'Footer'(('Perimeter=',⍕n×s),(' Radius=',⍕4⍕radius),(' Apothem=',⍕4⍕apothem),(' Area=',⍕4⍕area))
ch.Set¨(⊂¨'Xrange' 'Yrange'),¨range←⊂¯1 1×⌈/|x,y
ch.Set¨('Xint' 0)('Yint' 0)('forcezero')('XYPLOT,GRID')
ch.Plot⍉↑x y
PG←ch.Close
R←'View PG ⍝ to see it'
Here is what I have for rainpro. I am keeping xy axes in until I get it really straight then I will probably drop them.
R←PolyPlot(n s);y;x;y;foot;range;x0;y0;Deg2Rad;theta;i;radius;py;px;pct;area;apothem
⍝ n is number of sides s=side length. So: PolyPlot 5 10 would plot a 5 sided polygon with each side=10
Deg2Rad←{⍵×○1÷180} ⍝ fns to convert degrees to radians for input to trigonometric fns
radius←s÷2×1○Deg2Rad 180÷n ⍝ center to a vertex 1○ is sine
apothem←s÷2×3○Deg2Rad 180÷n ⍝ center to midpt side 3○ is tangent
area←(n×s*2)÷4×3○Deg2Rad 180÷n ⍝ area of polygon 3○ is tangent
x0←y0←0 ⍝ x y location of center of polygon on plot
⍝ see http://www.mathopenref.com/polygonregulararea.html for explanation of following formulas
theta←(360÷n)×i←0,(⍳n-1),0 ⍝ theta is angle with the x axis for plot based on # of sides (n)
x←x0+radius×2○Deg2Rad theta+i×(2×○1)÷n ⍝ x vertice locations 2○ is cosine
y←y0+radius×1○Deg2Rad theta+i×(2×○1)÷n ⍝ y vertice locations 1○ is sine
ch.New 350 350 ⍝ trying to make x y lengths the same but failing
⍝ ch.Set'syle' 'noaxes' must shut off other stuff to make this work
ch.Set'Head'((⍕n),' Sided Polygon Plot side length=',⍕s)
ch.Set'Footer'(('Perimeter=',⍕n×s),(' Radius=',⍕4⍕radius),(' Apothem=',⍕4⍕apothem),(' Area=',⍕4⍕area))
ch.Set¨(⊂¨'Xrange' 'Yrange'),¨range←⊂¯1 1×⌈/|x,y
ch.Set¨('Xint' 0)('Yint' 0)('forcezero')('XYPLOT,GRID')
ch.Plot⍉↑x y
PG←ch.Close
R←'View PG ⍝ to see it'