Skip to content

Instantly share code, notes, and snippets.

@odoku
Created May 19, 2020 02:47
Show Gist options
  • Save odoku/99e06ede6430801b9dca24911b516886 to your computer and use it in GitHub Desktop.
Save odoku/99e06ede6430801b9dca24911b516886 to your computer and use it in GitHub Desktop.
from typing import Type, TypeVar, Union
import numpy as np
Number = TypeVar("TypeVar", int, float)
class Rectangle(np.ndarray):
def __new__(
cls,
x: Number,
y: Number,
width: Number,
height: Number,
) -> Type["Rectangle"]:
return np.asarray([x, y, width, height]).view(cls)
def resize(
self,
width_scale: Number,
height_scale: Number,
) -> "Rectangle":
rectangles = self.reshape((-1, 4))
rectangles = rectangles * (
width_scale, height_scale, width_scale, height_scale,
)
rectangles = rectangles.reshape(self.shape)
return rectangles
rs = np.asarray([
(10, 20, 30, 40.5),
(10, 20, 30, 40),
(10, 20, 30, 40),
(10, 20, 30, 40),
]).view(Rectangle)
print(rs.resize(0.5, 0.5))
print(rs * 2)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment