Module: Sunniesnow::Tools::SvgPath

Defined in:
lib/sscharter/tools/svg_path.rb

Overview

See Also:

Defined Under Namespace

Modules: Curve Classes: Arc, Command, CommandError, CubicBezier, GeometryError, Line, Path, PathSegment, QuadraticBezier

Constant Summary collapse

Vector2D =
Sunniesnow::Utils::Data.define :x, :y do

	# @!attribute [r] x
	#   @return [Float]
	# @!attribute [r] y
	#   @return [Float]

	# @return [Vector2D]
	def +@
		self
	end

	# @return [Vector2D]
	def -@
		Vector2D.new -x, -y
	end

	# @param other [Vector2D]
	# @return [Vector2D]
	def + other
		Vector2D.new x + other.x, y + other.y
	end

	# @param other [Vector2D]
	# @return [Vector2D]
	def - other
		Vector2D.new x - other.x, y - other.y
	end

	# @param other [Vector2D, Numeric]
	# @return [Vector2D]
	def * other
		other.is_a?(Vector2D) ? Vector2D.new(x*other.x, y*other.y) : Vector2D.new(x*other, y*other)
	end

	# @param other [Vector2D, Numeric]
	# @return [Vector2D]
	def / other
		other.is_a?(Vector2D) ? Vector2D.new(x/other.x, y/other.y) : Vector2D.new(x/other, y/other)
	end

	# @return [Array<Float>]
	def to_a
		[x, y]
	end

	# @return [Float]
	def length
		Math.hypot x, y
	end

	# @return [Float]
	def angle
		Math.atan2 y, x
	end

	# @return [Float]
	def length2
		x*x + y*y
	end

	# @param angle [Numeric] in radians
	# @return [Vector2D]
	def rotate angle
		cos = Math.cos angle
		sin = Math.sin angle
		Vector2D.new x*cos - y*sin, x*sin + y*cos
	end

	# @param str [String] in the format "x,y".
	# @return [Vector2D]
	def self.from_string str
		new *str.split(',').map(&:to_f)
	end

	# @param radius [Numeric]
	# @param angle [Numeric] in radians
	# @return [Vector2D]
	def self.from_polar radius, angle
		new Math.cos(angle)*radius, Math.sin(angle)*radius
	end
end