Last active
April 5, 2025 01:42
-
-
Save fsubal/54fa475a2c8e8358785b142a046ae564 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Vector2D < Vector | |
class Pixel < Vector2D; end | |
class Millimeter < Vector2D; end | |
class Viewbox < Vector2D; end | |
def initialize(*) | |
super | |
raise ArgumentError unless size == 2 | |
end | |
# 自分と同じクラスか、無次元のVector2Dが相手であれば加算や乗算ができる | |
# Millimeter * Pixel などはエラーになる | |
concerning :SameUnit do | |
def +(other) | |
case other | |
in self.class | Vector2D | |
super(other) | |
end | |
end | |
def *(other) | |
case other | |
in self.class | Vector2D | |
super(other) | |
end | |
end | |
end | |
def x | |
self[0] | |
end | |
alias width x | |
def y | |
self[1] | |
end | |
alias height y | |
def to_s | |
"#{x}x#{y}" | |
end | |
def to_h | |
{ x:, y: } | |
end | |
def to_a | |
[x, y] | |
end | |
end | |
def canvas_size_mm | |
Vector2D::Millimeter[100, 200] | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment