Skip to content

Commit

Permalink
docs & format: some files in book2
Browse files Browse the repository at this point in the history
  • Loading branch information
littlebutt committed May 24, 2024
1 parent c4f2492 commit 3566c56
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 20 deletions.
32 changes: 24 additions & 8 deletions book2/image.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,46 @@


class TextureImage:
'''
A tool class for loading image file.
Attributes:
filename: The image file.
'''
def __init__(self, filename: str) -> None:
self._load(filename)

def _load(self, filename: str) -> None:
image = Image.open(filename)
self.image_height = image.height
self.image_width = image.width
self.data = [[0 for i in range(self.image_width)] for j in range(self.image_height)]
self.data = [[0 for i in range(self.image_width)]
for j in range(self.image_height)]
_data = image.load()
for w in range(self.image_width):
for h in range(self.image_height):
r, g, b = _data[w, h]
self.data[h][w] = (r, g, b)

def pixel_data(self, x: int, y: int) -> "Color":
'''
Find pixel :class:`Color` with given coordinate.
Args:
x: The x coord.
y: The y coord.
Returns:
Color: The color info.
'''
x = self.clamp(x, 0, self.image_width)
y = self.clamp(y, 0, self.image_height)
return Color(x=self.data[y][x][0], y=self.data[y][x][1], z=self.data[y][x][2])

return Color(x=self.data[y][x][0],
y=self.data[y][x][1],
z=self.data[y][x][2])

@staticmethod
def clamp(x: int, low: int, high: int) -> int:
if x < low:
Expand All @@ -36,7 +56,3 @@ def clamp(x: int, low: int, high: int) -> int:
if __name__ == '__main__':
ti = TextureImage('./earthmap.jpg')
print(ti.data)




30 changes: 18 additions & 12 deletions book2/interval.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,27 +7,32 @@

class Interval:
'''
Util class for detecting the relation between the target and two :type:`int`.
Util class for detecting the relation between the target and two
:type:`int`.
Attributes:
min: The minimum float.
max: The maximum float.
a: One of :class:`Interval`s when it is made up by two
:class:`Intervals`.
b: Another :class:`Interval`.
'''

def __init__(self, min: float=-INFINITY, max: float=INFINITY,
a: Optional["Interval"]=None, b: Optional["Interval"]=None) -> None:
def __init__(self, min: float = -INFINITY, max: float = INFINITY,
a: Optional["Interval"] = None,
b: Optional["Interval"] = None) -> None:
self.min = min
self.max = max
# NOTE: `a` and `b` will cover the `min` and `max` if both
# NOTE: `a` and `b` will cover the `min` and `max` if both
# assigned. Use dict parameters if you want to pass to `a` and `b`.
if a is not None and b is not None:
self.min = a.min if a.min < b.min else b.min
self.max = a.max if a.max > b.max else b.max

def size(self) -> float:
'''The difference between the :arg:`max` and the :arg:`min`.'''
return self.max - self.min

def contains(self, x: float) -> bool:
'''
Detect if the target is in the :class:`Interval` with edges included.
Expand All @@ -37,17 +42,18 @@ def contains(self, x: float) -> bool:
'''
return x >= self.min and x <= self.max

def surrounds(self, x: float) -> bool:
'''
Detect if the target is out of the :class:`Interval` with edges excluded.
Detect if the target is out of the :class:`Interval` with edges
excluded.
Args:
x: The target.
'''
return x > self.min and x < self.max

def clamp(self, x: float) -> float:
'''
Return the clamped value of the target within the :class:`Interval`.
Expand All @@ -62,7 +68,7 @@ def clamp(self, x: float) -> float:
return self.max
else:
return x

def expand(self, delta: float) -> "Interval":
padding = delta / 2
return Interval(self.min - padding, self.max + padding)
Expand All @@ -71,4 +77,4 @@ def expand(self, delta: float) -> "Interval":
EMPTY = Interval(+INFINITY, -INFINITY)


UNIVERSE = Interval(-INFINITY, +INFINITY)
UNIVERSE = Interval(-INFINITY, +INFINITY)

0 comments on commit 3566c56

Please sign in to comment.