Wednesday, November 26, 2008

Python: draw an ellipse

The following Python script draws an ellipse.
from numpy import linspace
from scipy import pi,sin,cos


def ellipse(ra,rb,ang,x0,y0,Nb=50):
'''ra - major axis length
rb - minor axis length
ang - angle
x0,y0 - position of centre of ellipse
Nb - No. of points that make an ellipse

based on matlab code ellipse.m written by D.G. Long,
Brigham Young University, based on the
CIRCLES.m original
written by Peter Blattner, Institute of Microtechnology,
University of
Neuchatel, Switzerland, blattner@imt.unine.ch
'''
xpos,ypos=x0,y0
radm,radn=ra,rb
an=ang

co,si=cos(an),sin(an)
the=linspace(0,2*pi,Nb)
X=radm*cos(the)*co-si*radn*sin(the)+xpos
Y=radm*cos(the)*si+co*radn*sin(the)+ypos
return X,Y

def test():
import pylab as p

fig = p.figure(figsize=(5,5))
p.axis([-3,3,-3,3])

#eg 1
X,Y=ellipse(2,1,pi*2.0/3.0,0,1)
p.plot(X,Y,"b.-",ms=1) # blue ellipse

#eg 2
X,Y=ellipse(2,0.2,pi/3.0,1,1)
p.plot(X,Y,"r.-",ms=1) # red ellipse

#eg 3
X,Y=ellipse(1,1,pi/3.0,-1,1,Nb=16)
p.plot(X,Y,"g.-",ms=1) # green ellipse

p.grid(True)
p.show()


if __name__ == '__main__':
test()

Note

The script is in ellipse.py.

6 comments:

  1. Anonymous1:36 PM

    Useful, thank you.

    ReplyDelete
  2. Anonymous3:13 AM

    Make mittens and boots from your leftover black felt.
    You may use them like a great Xmas decoration concepts this coming year.
    Of course Christmas is essentially a celebration of faith, so if the nativity scene is
    central in your Xmas, then by all signifies be certain to contain it.



    my blog ... www.ideasforchristmasdecorating.com

    ReplyDelete
  3. Anonymous4:02 AM

    Therefore, people invest huge amounts of money in garden decoration to give it a more unique, and
    elegant touch. ' You can arrange the lights in your garden in many ways as garden decor in order to make your garden look more beautiful. Bundle dozens of twigs of the appropriate length, and place them inside.

    Look into my web blog ... http://www.gardenlandscapeideas.org/

    ReplyDelete
  4. Anonymous5:38 AM

    Hmm it seems like your site ate my first comment (it was
    super long) so I guess I'll just sum it up what I submitted and say, I'm thoroughly enjoying your blog.
    I as well am an aspiring blog blogger but I'm still new to the whole thing. Do you have any points for first-time blog writers? I'd certainly appreciate it.


    Here is my blog post ... quantrim fast track formula

    ReplyDelete
  5. Anonymous4:18 PM

    You can decorate the cake with seasonal flowers.
    Cooking magazines like Gourmet, Bon Appetit and Cooking Light are a good place to read about current wedding cake flavor trends.
    Your cluster of shells, for the reason that seashell cake mattress topper is a must
    in this cake structure.

    Feel free to visit my web site; www.cakedecoratingtrends.com

    ReplyDelete
  6. Anonymous7:45 PM

    Toronto wedding decor would never be complete without ribbons.
    These are some of the most easy to personalize wedding favors.

    Far too often brides decide they want a particular wedding theme and they stop there.


    My web page ... Outdoor Wedding Decorating Pictures Photograph

    ReplyDelete