Lines and Points¶
You can use draw_point() to draw a point. The size of point is determined by line width (pen size); If you want to draw a pixel, use put_pixel() instead.
You can use line() to draw a line.
Draw with the Current Position¶
In easygraphics, each image (including the graphics window) stores “a current position”. Use this position, we can draw lines relatively.
The related functions are:
||Get the x coordinate value of the current drawing position (x,y).|
||Get the y coordinate value of the current drawing position (x,y).|
||Get the current drawing position (x,y).|
||Set the drawing position to (x,y).|
||Move the drawing position by (dx,dy).|
||Draw a line from the current drawing position to (x,y), then set the drawing position is set to (x,y).|
||Draw a line from the current drawing position (x,y) to (x+dx,y+dy), then set the drawing position is set to (x+dx,y+dy).|
The following program draws a dash line by using the current position.
from easygraphics import * def main(): init_graph(400, 100) move_to(50, 50) for i in range(10): line_rel(10, 0) move_rel(20, 0) pause() close_graph() easy_run(main)
Approximate a function curve¶
Sometimes we need to draw line segments successively.
For example, to plot the function f(x)=sin(x)’s curve on [-3,3] ,we can use many successive line segements to approximate the curve:
- divide [-3,3] into n equal intervals, to get n+1 values evenly distributed on [-3,3]: x0,x1,x2,x3,…,xn, and x0=-3, xn=3
- cacluate function values f(x0),f(x1),f(x2),f(x3), …, f(xn).
- draw n line segements: (x0,f(x0)) to (x1,f(x1)), (x1,f(x1)) to (x2,f(x2)) …, (xn-1,f(xn-1)) to (xn,f(xn))
- the resulting line segments is the curve approximation we need.
Apparently, the more greater n is, the more precisely the appoximation is. To minimize the usage of memory, we should calculate and draw the line segments one by one.
The following program plot a sin(x) curve on [-3,3].
from easygraphics import * import math as m def main(): init_graph(600, 400) translate(300, 200) # move origin to the center scale(100, -100) # zoom each axis 100 times, and make y-axis grow from bottom to top. x = -3 delta = 0.01 move_to(x, m.sin(x)) while x <= 3: line_to(x, m.sin(x)) x = x + delta pause() close_graph() easy_run(main)