Sha256: cabcac550f44fc531d33aa70af2fbf93c4c152bf53c076c8cc64e4bb33a7d7bf

Contents?: true

Size: 690 Bytes

Versions: 3

Compression:

Stored size: 690 Bytes

Contents

#!/usr/bin/ruby -w

require "graphics"

class MathSimulation < Graphics::Simulation
  def initialize
    super 640, 640, 16, "Math"
  end

  def draw n
    graph_paper

    (0..w).each do |x|
      y = 2*x*x + 2*x - 12
      y /= 320.0

      next if y < 0
      next if y > h

      rect x, y, 3, 3, :red, :filled
    end

    text "2x^2 +2x -12", 10, h-font.height, :black
  end

  def graph_paper
    clear :white

    hline 1, :black
    vline 0, :black

    (0..w).step(25).each do |x|
      (0..h).step(25).each do |y|
        line 0, y, 5, y, :black if x == 0
        line x, 1, x, 6, :black if y == 0
        point x, h-y, :black
      end
    end
  end
end

MathSimulation.new.run

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
graphics-1.0.0b5 examples/math.rb
graphics-1.0.0b4 examples/math.rb
graphics-1.0.0b1 examples/math.rb