Skip to content

Instantly share code, notes, and snippets.

@ElanDeyan
Last active September 5, 2023 17:49
Show Gist options
  • Save ElanDeyan/f07c9adc608d61bcbac6dbbf617b4321 to your computer and use it in GitHub Desktop.
Save ElanDeyan/f07c9adc608d61bcbac6dbbf617b4321 to your computer and use it in GitHub Desktop.
primal-jungle-2630
void main() {
const a = Vec2(horizontalDimension: 2, verticalDimension: 3);
const b = Vec2(horizontalDimension: -4, verticalDimension: -2);
print(4.multiplyInVec2(a) - 2.multiplyInVec2(b));
}
final class Vec2 {
final num horizontalDimension;
final num verticalDimension;
const Vec2(
{required this.horizontalDimension, required this.verticalDimension,});
Vec2 operator +(Vec2 other) => Vec2(
horizontalDimension: horizontalDimension + other.horizontalDimension,
verticalDimension: verticalDimension + other.verticalDimension,
);
Vec2 operator -(Vec2 other) =>
this + (other.multipliedByScalar(-1));
Vec2 multipliedByScalar(num scalar) => Vec2(
horizontalDimension: scalar * horizontalDimension,
verticalDimension: scalar * verticalDimension,
);
@override
String toString() => '[$horizontalDimension,$verticalDimension]';
}
extension Vector2 on num {
Vec2 multiplyInVec2(Vec2 vector) => Vec2(
horizontalDimension: this * vector.horizontalDimension,
verticalDimension: this * vector.verticalDimension,
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment