2a2967ad1f03472bee5f9b2918ddaa60 by 2024

2a2967ad1f03472bee5f9b2918ddaa60 by 2024

Author:2024
Language: eng
Format: epub


Having fun with the pixels

When learning computer graphics, before jumping to complicated algorithms like 3D rendering, there are some other stuff you can do to leverage your power of manipulating pixels.

There are plenty of things you can draw on a 2D surface. Let’s start with drawing a simple 2D line. A 2d line may be defined by its slope and offset: y = ax + b

Given two points, you can find the corresponding a and b that passes through this line too. Assuming the points are: P 1 = ( x 1 , y 1) and P 2 = ( x 2 , y 2): a = y 2− y 1

x 2− x 1

b = − ax 2 + y 2

Let’s have a class for working with lines!

class Line:

def __init__(self, slope: float, offset: float): self.slope = slope

self.offset = offset

def get_y(self, x: float):

return self.slope * x + self.offset

@staticmethod

def from_points(a: Vec2D, b: Vec2D) -> Line: slope = (b.y - a.y) / (b.x - a.x)

offset = -slope * b.x + a.x

return Line(slope, offset)

Here is something interesting to draw with simple lines:

[TODO]



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.