Skip to content

Reimann Integration visualization using Mayavi2

December 26, 2010

As I said Reimann Integration in https://dhastha.wordpress.com/2010/12/14/introduction-to-reimann-integration/ , It used to find the area of curved object. What I done is that, we all are know the area of circle is pi*r2.Without using this formula am going to find area of circle and I will display how it comes using Mayavi2.

Procedure:

1. Draw a circle object with radius

2. Divide the circle object into n number of equal sized inscribed triangles and suprimum triangles.

3. Find the area of one inscribed triangle using the formula 1/2*base*height. Then multiply the area with n to find the total area occupied by Inscribed triangles

4. Find the area of one out scribed triangle using the formula 1/2*base*height. Then multiply the area with n to find the total area occupied by out scribed triangles

5. Take average between area of  these two inscribed and out scribed triangles it will probably equals to area of circle

6. Find the area of one chord, using this chord find the area of outer piece of place which is not inside the circle

To run the following program, you must install Mayavi2 on your system.

To install Mayavi2 on Ubuntu (sudo apt-get install mayavi2)

Copy the following program save it as yourfilename.py. Run in terminal as

python yourfilename.py

Program:

import numpy as np
from enthought.mayavi import mlab
from enthought.tvtk.api import tvtk

v=mlab.figure()  #create new mayavi scene

x0,y0,z0=0,0,0         #initial points
r=input("Enter radius of your circle:")         #get the values of radius, number of slice to divide thecircle
n=input("Enter how many slice:")
a=n*100

#To plot the circle
t = np.linspace(0, 2*np.pi, a)
x = x0 + r*np.cos(t)
y = y0 + r*np.sin(t)
z = z0*np.ones_like(y)
mlab.plot3d(x,y,z,tube_radius=None)

#find the points in circle to draw line
t1 = np.linspace(0, 2*np.pi, a)
x1 = x0 + r*np.cos(t1)
y1 = y0 + r*np.sin(t1)
z1 = z0*np.ones_like(y)

#inscribed triangle

line = tvtk.LineSource(point1=(x0,y0,z0), point2=(x1[0], y1[0], z1[0]))
lineMapper = tvtk.PolyDataMapper(input=line.output)
lineActor = tvtk.Actor(mapper=lineMapper)
v.scene.add_actor(lineActor)

g=a/n
g1=g
i=1
b=0
while i < n:
    line1 = tvtk.LineSource(point1=(x0,y0,z0), point2=(x1[g], y1[g], z1[g]))
    lineMapper = tvtk.PolyDataMapper(input=line1.output)
    lineActor = tvtk.Actor(mapper=lineMapper)
    v.scene.add_actor(lineActor)
    line2 = tvtk.LineSource(point1=(x1[b], y1[b], z1[b]), point2=(x1[g], y1[g], z1[g]))
    lineMapper = tvtk.PolyDataMapper(input=line2.output)
    lineActor = tvtk.Actor(mapper=lineMapper)
    v.scene.add_actor(lineActor)
    i=i+1
    b=g
    g=g+g1
    

line2 = tvtk.LineSource(point1=(x1[b], y1[b], z1[b]), point2=(x1[0], y1[0], z1[0]))
lineMapper = tvtk.PolyDataMapper(input=line2.output)
lineActor = tvtk.Actor(mapper=lineMapper)
v.scene.add_actor(lineActor)   

#area of one inscribed triangle
base1=np.sqrt((x1[0]-x1[b])*(x1[0]-x1[b])+(y1[0]-y1[b])*(y1[0]-y1[b])+(z1[0]-z1[b])*(z1[0]-z1[b]))
area1=(base1*r)/2
total_area=n*area1

#prepare points to draw outer triangle. The radius r2 is derived from pythagoras theorm
r2=r/np.cos(np.pi/n)
t2 = np.linspace(0, 2*np.pi, a)
x2 = x0 + r2*np.cos(t2)
y2 = y0 + r2*np.sin(t2)
z2 = z0*np.ones_like(y)

#suprimum triangle
#plot first line for suprimum triangle
line = tvtk.LineSource(point1=(x0,y0,z0), point2=(x2[0], y2[0], z2[0]))
lineMapper = tvtk.PolyDataMapper(input=line.output)
lineActor = tvtk.Actor(mapper=lineMapper)
v.scene.add_actor(lineActor)

g=a/n         #to know arc distance

#area of outscribed triangle

area2=g
g1=g
i=1
b=0
while i < n:
    line1 = tvtk.LineSource(point1=(x0,y0,z0), point2=(x2[g], y2[g], z2[g]))
    lineMapper = tvtk.PolyDataMapper(input=line1.output)
    lineActor = tvtk.Actor(mapper=lineMapper)
    v.scene.add_actor(lineActor)
    line2 = tvtk.LineSource(point1=(x2[b], y2[b], z2[b]), point2=(x2[g], y2[g], z2[g]))
    lineMapper = tvtk.PolyDataMapper(input=line2.output)
    lineActor = tvtk.Actor(mapper=lineMapper)
    v.scene.add_actor(lineActor)
    i=i+1
    b=g
    g=g+g1
    

line2 = tvtk.LineSource(point1=(x2[b], y2[b], z2[b]), point2=(x2[0], y2[0], z2[0]))
lineMapper = tvtk.PolyDataMapper(input=line2.output)
lineActor = tvtk.Actor(mapper=lineMapper)
v.scene.add_actor(lineActor)


#area of suprimum triangle
base2=np.sqrt((x2[0]-x2[b])*(x2[0]-x2[b])+(y2[0]-y2[b])*(y2[0]-y2[b])+(z2[0]-z2[b])*(z2[0]-z2[b]))
area2=(r*base2)/2
total_area1=n*area2

print "Total area of Inscribed triangle is:"
print total_area

print "Total area of suprimum triangle is:"
print total_area1

average=(total_area+total_area1)/2
print "Average between Inscribed and suprimum triangle is:"
print average

#find the area of original circle using formula pi*r*r
cir_area=np.pi*r*r
print "Area of original circle:"
print cir_area


#finding the area of one chord
angle=(2*np.pi)/n
chord_area=((r*r)*(angle-np.sin(angle)))/2
print "Area of one chord area"
print chord_area

#find the total area of slice of suprimum triangle which is not inside the circle
q=area2-area1
t=q-chord_area
print "Area of small space of outer triangle which is not inside of the circle:"
print n*t
mlab.show()

Output:

Enter radius of your circle:2
Enter how many slice:10


Total area of Inscribed triangle is:
12.2529607843
Total area of suprimum triangle is:
12.8835253997
Average between Inscribed and suprimum triangle is:
12.568243092
Area of original circle:
12.5663706144
Area of one chord area
0.081066556851
Area of small space of outer triangle which is not inside of the circle:
0.180100953047

 

The area of original circle is probably equal to average area of inscribed triangles and out scribed triangles.

Various outputs for different n number of slice:

I will use Traits to create GUI for this program.

2 Comments leave one →
  1. January 5, 2011 5:47 am

    Hey dude,

    Its really awesome.

    Really great man.!.

    Nice to see the visualisation of circles inside/outside triangle in mayavi…

    Wishes for your project…keep rocking…

Trackbacks

  1. Reimann Integration with Mayavi2 and traits « Li(G)NUx

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: