# ! /Usr/bin/ENV Python
# Coding = UTF-8
# -------------------------------------------------------------------------------
# Name: simple function drawing class
# Version: v0.1
# Author: mapig (http://mapig.cnblogs.com QQ: 33221979)
# Power: generate simple function Images
# Creation:
# Standby Note: Python imaging Library (PIL) is required)
# -------------------------------------------------------------------------------
Import Math
Import Image
Import Imagedraw
Class Drawfun ():
# Canvas pixel size
Width = 512
Height = 512
# X value range
Min = - 10
Max = 10
# Step value of X
Step = 1
# X unit value Pixel
Unit = 1
# Canvas object
Canvas = None
# Paint Brush object
Draw = None
# Canvas center value offset
Ctrx = 0
Ctry = 0
Def _ Init __ (Self, width, height, Min, Max, step, unit ):
Self. Width = Width
Self. Height = Height
Self. Min = Min
Self. Max = Max
Self. Step = Step
Self. Unit = Unit
# Create a canvas object
Self. Canvas = Image. New ( " RGB " , [Self. Width, self. Height], ( 255 , 255 , 255 ))
# Create a paint brush object
Self. Draw = Imagedraw. Draw (self. Canvas)
# Obtain the canvas Center
Self. ctrx = Math. Floor (self. Width / 2 )
Self. ctry = Math. Floor (self. Height / 2 )
# Draw central axis
Self. Draw. Line ([(0, self. ctry), (self. Width, self. ctry)], fill = (0, 0 ))
Self. Draw. Line ([(self. ctrx, 0), (self. ctrx, self. Height)], fill = (0, 0 ))
# Symmetric calculation of the number of displayed Cells
Viewx = INT (math. Floor (self. Width / Self. Unit + 1 ) / 2 ))
Viewy = INT (math. Floor (self. Height / Self. Unit + 1 ) / 2 ))
# Draw Cells
For X In Range ( 1 , Viewx + 1 ):
Offsetx = X * Self. Unit
Self. Draw. Line ([(self. ctrx + Offsetx, 0), (self. ctrx + Offsetx, self. Height)], fill = ( 200 , 200 , 200 ))
Self. Draw. Line ([(self. ctrx - Offsetx, 0), (self. ctrx - Offsetx, self. Height)], fill = ( 200 , 200 , 200 ))
For Y In Range ( 1 , Viewy + 1 ):
Offsety = Y * Self. Unit
Self. Draw. Line ([(0, self. ctry + Offsety), (self. Width, self. ctry + Offsety)], fill = ( 200 , 200 , 200 ))
Self. Draw. Line ([(0, self. ctry - Offsety), (self. Width, self. ctry - Offsety)], fill = ( 200 , 200 , 200 ))
Def Paint (self, fun, color ):
# Result set
Arypoint = []
# Convert to 1 as step size
Min = INT (math. Floor (self. Min / Self. Step ))
Max = INT (math. Floor (self. Max / Self. Step ))
For I In Range (Min, max ):
# Actual function value
X = I * Self. Step
Y = Fun (X)
# Cartesian coordinate system to screen Coordinate System
# Proportional Transformation
X = X * Self. Unit
Y = Y * Self. Unit
# Coordinate Transformation
Curx = Self. ctrx + X
Cury = Self. ctry - Y
# Save coordinates to the array
Arypoint. append (curx, Cury ))
# Draw Functions
Self. Draw. Point (arypoint, fill = Color)
# Show Image
Def Show (Self ):
# Show Image
Self. Canvas. Show ()
# Save to JPG
Def Savetojpg (self, PATH ):
Self. Canvas. Save (path, " JPEG " )
# Save to PNG
Def Savetopng (self, PATH ):
Self. Canvas. Save (path, " PNG " )
DefSin (x ):
ReturnMath. Sin (X)
DefCos (x ):
ReturnMath. Cos (X)
DefTan (x ):
ReturnMath. Tan (X)
If _ Name __ = " _ Main __ " :
# Drawfun ("canvas width", "canvas height", "x minimum", "x maximum", "x step size", "x unit value pixel size ")
Drawfun = Drawfun ( 512 , 512 , - 10 , 10 , 0.001 , 50 )
# Drawfun. Paint (f (x) function, RGB color)
Drawfun. Paint (sin ,( 255 , 0, 0 ))
Drawfun. Paint (COS, (0, 255 , 0 ))
Drawfun. Paint (tan, (0, 0, 255 ))
# Show function Images
Drawfun. Show ()
# Save to image
Drawfun. savetojpg ( " Demo.jpg " )
Drawfun. savetopng ( " Demo.png " )
Generate results