Understanding the Straight Line Equation in Python
In this tutorial, you'll learn how to draw and animate a straight line in Python using the slope-intercept form of a line. We'll explore how to calculate the equation of a straight line, plot it on a 2D coordinate plane, and even animate a point moving along the line using Python's graphics tools such as Turtle. Whether you're studying coordinate geometry or working on a Python graphics project, this guide will help you understand linear motion in a clear, visual way.
                    
                        In coordinate geometry, whether for use in Python or any other language, the equation of a 
                        straight line has the general form y = mx + c;
                        where m is the slope and c is the intercept on the y-axis.
                    
                        For a vertical line, x is constant and for a horizontal line, y is constant.
                        This formula helps in calculating and drawing straight lines in Python, 
                        whether for graphics, animations, or math-based programming.
                    
Example: Finding the Line Equation Between Two Points | Maths Explanation for Python Kids
                        In Python, you can formulate line equation using two known points:
                        Given any 2 points on the Python Canvas (x1, y1)
                        and (x2, y2); we'll have:
                    
| y2 - y1 | = | y - y1 | |
| x2 - x1 | x - x1 | ||
| ⇒ y = ( | y2 - y1 | ) x + | x2y1 - x1y2 | 
| x2 - x1 | x2 - x1 | 
Comparing this linear equation, for the given Python canvas points, to the general equation of a straight line, i.e. y = mx + c
| m = | y2 - y1 | 
| x2 - x1 | |
| & | |
| c = | x2y1 - x1y2 | 
| x2 - x1 | 
                        Say we are to find the equation for the line passing through the arbitrary points (50, 50) and (200, 100) on a Python canvas:
                    
| m = | 100 - 50 | = | 50 | = | 1 | 
| 200 - 50 | 150 | 3 | |||
| & | |||||
| c = | 200(50) - 50(100) | = | 10000 - 5000 | ||
| 200 - 50 | 150 | ||||
| = | 5000 | = | 100 | ||
| 150 | 3 | ||||
                        Hence,
         
                        y = 1/3x + 100/3
                        
or
        
                        3y = x + 100
                    
This gives a Python-ready straight line equation that you can use to animate objects or draw lines on a canvas.
Python Code Example - Animate Object Along a Straight Line
To animate a dot along a straight line in Python, we can increment the x-coordinate and compute the matching y-coordinate using the equation of the line.
Let's do so with the above equation representing points (x1, y1) = (50, 50) and (x2, y2) = (100, 200).
                        Create 2 new Python files; File, New File.
                        Call them Facet.py and StraightLine.py.
                        Type out the adjoining Python / Turtle code for animating an image 
                        body through the path of a straight line.
                    
Important: When trying to click on the button to get things started, you might need to click away from the button text.
Summary: Visualizing Linear Equations in Python
The straight line equation is one of the first concepts students learn in senior secondary mathematics. In Python, we can easily plot a line by defining its slope (m) and intercept (c). This Python maths tutorial demonstrates how to compute the equation of a line given two points and visualize it using code.
                        Using Python to draw straight lines helps students understand the relationship 
                        between slope and intercept in linear equations.
                        We've learnt how to draw a straight line in Python using the equation y = mx + c.
                        This lesson explained how to calculate slope and intercept, plot the straight line equation in Python,
                        and even animate a moving dot along the line using Python's Turtle library.
                        This foundation helps you transition into more advanced Python graphics and animation projects.
                    
So! Python Fun Practice Exercise - Animate in Straight Line
As a fun practice exercise, try modifying the Python code to explore different gradients and intercepts. This will be a great way to connect mathematics and programming, and help you understand more about Python animations and linear equations.
Python Straight Line Code for Turtle Template - Facet Class
class Template:
def __init__(self):
screen = turtle.getscreen()
screen.title("usingMaths.com")
screen.setup(width=0.9, height=0.9, startx=None, starty=None)
screen.colormode(255)
self.bgcolour = "#cccccc"
screen.bgcolor(self.bgcolour)
screen.delay(0)
self.screen = screen
self.wnd_width = self.screen.window_width()
self.wnd_height = self.screen.window_height()
def controlButtons(self):
self.button = turtle.clone()
turtle.speed(0)
turtle.penup()
turtle.setposition(-self.wnd_width/2, self.wnd_height/2)
turtle.color("black", "pink")
turtle.pendown()
# draw button region
turtle.begin_fill()
turtle.forward(self.wnd_width)
turtle.right(90)
turtle.forward(50)
turtle.right(90)
turtle.forward(self.wnd_width)
turtle.right(90)
turtle.forward(50)
turtle.end_fill()
self.button.speed(0)
self.button.penup()
# draw button
self.button.setposition(0, self.wnd_height/2 - 25)
self.button.shape("square")
self.button.resizemode("user")
self.button.shapesize(2, 8)
self.button.color("black", (255, 0, 255))
return turtle.getturtle()
Python Animation Code for Straight Line class
from Facet import Template
def prep():
global x1, y1, x_dot, y_dot, m, c, restart
restart = False
# bind fun function
scene.button.onrelease(play)
screen.delay(20)
turtle.penup()
# button text
turtle.setposition(scene.button.xcor(), scene.button.ycor()-10)
turtle.pendown()
turtle.write("Move", align="center", font=("Arial",16,"bold"))
# dot parameters
diameter = 1
dot_colour = "#ffff00"
x1 = 30 - scene.wnd_width/2; y1 = scene.wnd_height/3
x2 = scene.wnd_width/2 - 30; y2 = -scene.wnd_height/3
x_dot = x1; y_dot = y1
m = (y2 - y1) / (x2 - x1)
c = (x2*y1 - x1*y2) / (x2 - x1)
# transform turtle into a dot
turtle.penup()
turtle.setposition(x_dot, y_dot)
turtle.setheading(0)
turtle.shape("classic")
turtle.shapesize(diameter, diameter)
turtle.color(dot_colour, dot_colour)
# fun function when button is clicked
# just moves turtle until it hits the right boundary
def play(x, y):
global x_dot, y_dot, m, c, x1, y1, restart
if restart:
turtle.clearstamps()
restart = False
if x_dot < scene.wnd_width/2 - 10*turtle.shapesize()[1]:
y_dot = m*x_dot + c
# change dot's coordinates
turtle.setposition(x_dot, y_dot)
turtle.stamp()
x_dot += 20
screen.ontimer(play(0,0), 500)
else:
x_dot = x1; y_dot = y1
restart = True
screen.mainloop()
# Let fly
scene = Template();
turtle = scene.controlButtons()
screen = scene.screen
prep()