Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: master
Fetching contributors…

Octocat-spinner-32-eaf2f5

Cannot retrieve contributors at this time

file 189 lines (161 sloc) 3.825 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188
#!/usr/bin/env ruby
require 'example_helpers'
require 'set'

module Plots
  extend ColorTopology
  include Math
  black = BlockColor.typeColor[BlockColor::InvertedColor[:black]].rgb
  OrderedColors = BlockColor.typeColor.sort_by { |b| squaredColorDist(b.rgb, black) }

  def halfSphere
    plotWith :ice do |x, z|
      sqrt(18**2 - x**2 - z **2) + 30
    end
  end

  def paraboloid
    plot(proc {|x, z| (x** 2 + z ** 2) / 3}, proc do |b, z, x, y|
        b.name = :wool; b.data = OrderedColors[y * 16 / 128].data
      end)
  end

  def rotatedSine
    plotWith :gold do |x, z|
      sin(sqrt((x** 2 + z ** 2)) / 2) * 10 + 30
    end
  end

  def hyperbolicParaboloid
    plotWith :water do |x, z|
      (x** 2 - z ** 2) / 3 + 50
    end
  end

  def cone
    plotWith :diamond_block do |x, z|
      sqrt((x** 2 + z ** 2) / 3) * 5 + 20
    end
  end

  def gnuplotSurface10
    plotWith :lava do |x, z|
      log(x ** 4 * z ** 2 + 2) + 20
    end
  end

  def gnuplotSurface15
    plotWith :netherrack do |x, z|
      (sin(sqrt(z ** 2 + z ** 2)) / sqrt(x ** 2 + z ** 2)) * 30 + 30
    end
  end

  def polynomial
    plotWith :log do |x, z|
      x /= 5
      z /= 5
      (x + z) ** 5 + x**3 + z**2 + 30
    end
  end

  def polynomialQuotient
    plotWith :obsidian do |x, z|
      p1 = (x + z) ** 6 - x ** 3 + z **2 + 50
      p2 = x ** 7 + 6* z ** 6 - x **4 - z**2 + 30
      p1 / p2 + 10
    end
  end
end


# Simple functioning plotting
class PlottingExample
  include Bresenham
  include Plots
  attr_accessor :size
  attr_reader :height
  def initialize(regionFile)
    @z = padding
    @x = padding
    @y = 1
    @size = 3 * 16
    @height = 127
    @regionFile = regionFile
    @region = Region.fromFile(regionFile)
  end

  def width
    size - 2 * padding
  end

  def length
    size - 2 * padding
  end

  # for uniform plots
  def plotWith(fillBlock, &block)
    plot block, proc { |b| b.name = fillBlock}
  end

  def plot(function, fillFunction)
    cube = getCube
    middlePointX = length / 2
    middlePointZ = width / 2
    centeredF = proc do |x, z|
      function.call(x - middlePointX, z - middlePointZ).ceil
    end
    points = Set.new
    yzraster(centeredF, points)
    yxraster(centeredF, points)
    modifyBlocks(cube, centeredF, fillFunction, points)
  end



  def save
    @region.exportToFile @regionFile
  end

  protected
  def modifyBlocks(cube, centeredF, fillFunction, points)
    cube.each do |b, z, x, y|
      if points.include?([z, x, y])
        args = [b, z, x, y].take(fillFunction.arity)
        fillFunction.call *args
      end
    end
  end

  def yxraster(centeredF, points)
    raster do |z, x|
      p1 = point(x, centeredF.call(x, z))
      p2 = point(x, centeredF.call(x + 1, z))
      for newx, y in draw_line p1, p2
        points.add [z,newx, y]
      end
    end
  end

  def raster(&block)
    for z in 0..width
      for x in 0..length
        begin
          block.call(x, z)
        rescue Exception => ex
          # Ignroring infinites, complex numbers and non defined points
        end
      end
    end
  end

  def yzraster(centeredF, points)
    raster do |z, x|
      p1 = point(z, centeredF.call(x, z))
      p2 = point(z, centeredF.call(x, z + 1))
      for newz, y in draw_line p1, p2
        points.add [newz, x, y]
      end
    end
  end

  def getCube
    ret = @region.cube @z, @x, @y, :width => width, :length => length, :height => height
    @x += size
    if @x >= 3 * size
      @x = 0
      @z += size
    end
    return ret
  end

  def padding
    5
  end
end


if __FILE__ == $0
  puts "Starting"
  reg = '/home/daniel/.minecraft/saves/LowDirt/region/r.0.0.mcr'
  p = PlottingExample.new reg
  for type in Plots.public_instance_methods
    puts "Plotting #{type}"
    p.send type
  end
  puts 'exporting'
  p.save
  puts 'done'
end
Something went wrong with that request. Please try again.