#!/usr/bin/env python from collections import namedtuple from math import sqrt def mix(a, b, amount): return (1-amount)*a + amount*b def clamp(x, min, max): if x < min: return min if x > max: return max return x class Vec2(namedtuple('Vec2', ('x', 'y'))): def __add__(self, other): return Vec2(self.x+other.x, self.y+other.y) def __sub__(self, other): return Vec2(self.x+other.x, self.y+other.y) def __neg__(self): return Vec2(-self.x, -self.y) def __mul__(self, other): if isinstance(other, Vec2): return other.x*self.x + other.y*self.y else: return Vec2(other*self.x, other*self.y) def __div__(self, k): return Vec2(self.x/k, self.y/k) def length(self): return sqrt(self.x*self.x + self.y*self.y) def normalized(self): return self/self.length() def dist(self, other): return (other - self).length() def cross(self, other): return self.x*other.y - self.y*other.x def mix(self, other, amount): return Vec2(mix(self.x, other.x, amount), mix(self.y, other.y, amount)) def clamped(self, min, max): return Vec2(clamp(self.x, min.x, max.x), clamp(self.y, min.y, max.y)) Vec2.__radd__, Vec2.__rsub__, Vec2.__rmul__ = Vec2.__add__, Vec2.__sub__, Vec2.__mul__