Create a new Python script called LinePlot.py in PyCharm and try the following code:
import pygame
pygame.init()
screen_width = 1000
screen_height = 800
screen = pygame.display.set_mode((screen_width,
screen_height))
done = False
white = pygame.Color(255, 255, 255)
green = pygame.Color(0, 255, 0)
xoriginoffset = int(screen.get_width() / 2)
yoriginoffset = int(screen.get_height() / 2)
while not done:
for event in pygame.event.get():
if event.type == pygame.QUIT:
done = True
# x axis
for x in range(-500, 500):
screen.set_at((x + xoriginoffset, yoriginoffset),
green)
# y axis
for y in range(-400, 400):
screen.set_at((xoriginoffset, y + yoriginoffset),
green)
for x in range(-500, 500):
y = 2 * x + 4 #LINE EQUATION
screen.set_at((x + xoriginoffset, y +
yoriginoffset), white)
pygame.display.update()
pygame.quit()
This is plotting the same original line equation we used in the DESMOS exercise in Chapter 2, Let's Start Drawing (you can see it in Figure 2.3, albeit upside down because y is flipped). The xoriginoffset and yoriginoffset values are used to place the origin in the center of the window. You will notice that there are also green pixels drawn to show the $\mathrm{x}$ and $\mathrm{y}$ axes. The result is shown in Figure 3.2:
(Figure can't copy)
Besides the image being upside down in the Python render, the line is the same as the one drawn in DESMOS. Or is it? With high-resolution monitors, it can be difficult to see the issue that arises from plotting a line in this way. So, let's try something else.