This documentation is automatically generated by online-judge-tools/verification-helper

:warning: src/geometric/object/line.cr

Depends on

Required by

Code

require "../real"
require "./vec2"

module Geometric
  private module LineBase
    getter begin : Vec2, end : Vec2

    def initialize(@begin, @end)
    end

    def initialize(begin_x, begin_y, end_x, end_y)
      @begin = Vec2.new(begin_x, begin_y)
      @end = Vec2.new(end_x, end_y)
    end

    def +(v : Vec2)
      self.class.new(@begin + v, @end + v)
    end

    def -(v : Vec2)
      self.class.new(@begin - v, @end - v)
    end

    def vec
      @end - @begin
    end

    def counter_vec
      @begin - @end
    end

    def parallel?(other : LineBase)
      vec.cross(other.vec).sign == 0
    end

    def orthogonal?(other : LineBase)
      vec.dot(other.vec).sign == 0
    end

    def to_tuple
      {@begin, @end}
    end

    def inspect(io : IO) : Nil
      io << '(' << @begin << ", " << @end << ')'
    end
  end

  struct Line
    include LineBase

    def self.scan(s, io : IO) : self
      new Real.scan(s, io), Real.scan(s, io), Real.scan(s, io), Real.scan(s, io)
    end
  end

  struct Segment
    include LineBase

    def self.scan(scanner, io : IO) : self
      new Real.scan(s, io), Real.scan(s, io), Real.scan(s, io), Real.scan(s, io)
    end
  end
end
# require "../real"
require "big"

alias Real = BigFloat
EPS = Real.new(1e-12)

struct Real
  def <=>(other : Real)
    {% if Real == BigFloat %}
      if previous_def(other - EPS) < 0
        -1
      elsif previous_def(other + EPS) > 0
        1
      else
        0
      end
    {% else %}
      if self < other - EPS
        -1
      elsif self > other + EPS
        1
      else
        0
      end
    {% end %}
  end

  def sign : Int32
    self < -EPS ? -1 : self > EPS ? 1 : 0
  end

  def to_radian
    self * Math::PI / 180
  end

  def to_degree
    self * 180 / Math::PI
  end

  def self.scan(scanner, io : IO) : self
    Real.new scanner.s(io)
  end
end

# require "./vec2"
# require "../real"

module Geometric
  struct Vec2
    include Comparable(Vec2)

    property x : Real, y : Real

    def self.zero
      Vec2.new(Real.zero, Real.zero)
    end

    def initialize(x, y)
      @x, @y = Real.new(x), Real.new(y)
    end

    def self.scan(scanner, io : IO) : self
      Vec2.new(scanner.f(io), scanner.f(io))
    end

    def +
      self
    end

    def -
      Vec2.new(-x, -y)
    end

    {% for op in %w[+ - * /] %}
      def {{op.id}}(other : Vec2)
        Vec2.new(x {{op.id}} other.x, y {{op.id}} other.y)
      end

      def {{op.id}}(other)
        Vec2.new(x {{op.id}} other, y {{op.id}} other)
      end
    {% end %}

    def <=>(other : Vec2)
      x_cmp = x <=> other.x
      if x_cmp != 0
        x_cmp
      else
        y <=> other.y
      end
    end

    def [](index : Int)
      return x if index == 0
      return y if index == 1
      raise IndexError.new
    end

    def length : Real
      Math.hypot(x, y)
    end

    def dot(other : Vec2) : Real
      x * other.x + y * other.y
    end

    def cross(other : Vec2) : Real
      x * other.y - y * other.x
    end

    def angle : Real
      Real.new Math.atan2(y, x)
    end

    def rotate(rad : Real) : Vec2
      c, s = Math.cos(rad), Math.sin(rad)
      Vec2.new(x * c - y * s, x * s + y * c)
    end

    def to_tuple
      {x, y}
    end

    def inspect(io : IO)
      io << '(' << x << ", " << y << ')'
    end
  end
end

module Geometric
  private module LineBase
    getter begin : Vec2, end : Vec2

    def initialize(@begin, @end)
    end

    def initialize(begin_x, begin_y, end_x, end_y)
      @begin = Vec2.new(begin_x, begin_y)
      @end = Vec2.new(end_x, end_y)
    end

    def +(v : Vec2)
      self.class.new(@begin + v, @end + v)
    end

    def -(v : Vec2)
      self.class.new(@begin - v, @end - v)
    end

    def vec
      @end - @begin
    end

    def counter_vec
      @begin - @end
    end

    def parallel?(other : LineBase)
      vec.cross(other.vec).sign == 0
    end

    def orthogonal?(other : LineBase)
      vec.dot(other.vec).sign == 0
    end

    def to_tuple
      {@begin, @end}
    end

    def inspect(io : IO) : Nil
      io << '(' << @begin << ", " << @end << ')'
    end
  end

  struct Line
    include LineBase

    def self.scan(s, io : IO) : self
      new Real.scan(s, io), Real.scan(s, io), Real.scan(s, io), Real.scan(s, io)
    end
  end

  struct Segment
    include LineBase

    def self.scan(scanner, io : IO) : self
      new Real.scan(s, io), Real.scan(s, io), Real.scan(s, io), Real.scan(s, io)
    end
  end
end
Back to top page