Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 189 lines (161 sloc) 3.825 kB
6e1c68f @danielribeiro roller coaster the sine!
authored
1 #!/usr/bin/env ruby
2 require 'example_helpers'
3 require 'set'
4
5 module Plots
6 extend ColorTopology
7 include Math
8 black = BlockColor.typeColor[BlockColor::InvertedColor[:black]].rgb
9 OrderedColors = BlockColor.typeColor.sort_by { |b| squaredColorDist(b.rgb, black) }
10
11 def halfSphere
12 plotWith :ice do |x, z|
13 sqrt(18**2 - x**2 - z **2) + 30
14 end
15 end
16
17 def paraboloid
18 plot(proc {|x, z| (x** 2 + z ** 2) / 3}, proc do |b, z, x, y|
19 b.name = :wool; b.data = OrderedColors[y * 16 / 128].data
20 end)
21 end
22
23 def rotatedSine
24 plotWith :gold do |x, z|
25 sin(sqrt((x** 2 + z ** 2)) / 2) * 10 + 30
26 end
27 end
28
29 def hyperbolicParaboloid
30 plotWith :water do |x, z|
31 (x** 2 - z ** 2) / 3 + 50
32 end
33 end
34
35 def cone
36 plotWith :diamond_block do |x, z|
37 sqrt((x** 2 + z ** 2) / 3) * 5 + 20
38 end
39 end
40
41 def gnuplotSurface10
42 plotWith :lava do |x, z|
43 log(x ** 4 * z ** 2 + 2) + 20
44 end
45 end
46
47 def gnuplotSurface15
48 plotWith :netherrack do |x, z|
49 (sin(sqrt(z ** 2 + z ** 2)) / sqrt(x ** 2 + z ** 2)) * 30 + 30
50 end
51 end
52
53 def polynomial
54 plotWith :log do |x, z|
55 x /= 5
56 z /= 5
57 (x + z) ** 5 + x**3 + z**2 + 30
58 end
59 end
60
61 def polynomialQuotient
62 plotWith :obsidian do |x, z|
63 p1 = (x + z) ** 6 - x ** 3 + z **2 + 50
64 p2 = x ** 7 + 6* z ** 6 - x **4 - z**2 + 30
65 p1 / p2 + 10
66 end
67 end
68 end
69
70
71 # Simple functioning plotting
72 class PlottingExample
73 include Bresenham
74 include Plots
75 attr_accessor :size
76 attr_reader :height
77 def initialize(regionFile)
78 @z = padding
79 @x = padding
80 @y = 1
81 @size = 3 * 16
82 @height = 127
83 @regionFile = regionFile
84 @region = Region.fromFile(regionFile)
85 end
86
87 def width
88 size - 2 * padding
89 end
90
91 def length
92 size - 2 * padding
93 end
94
95 # for uniform plots
96 def plotWith(fillBlock, &block)
97 plot block, proc { |b| b.name = fillBlock}
98 end
99
100 def plot(function, fillFunction)
101 cube = getCube
102 middlePointX = length / 2
103 middlePointZ = width / 2
104 centeredF = proc do |x, z|
105 function.call(x - middlePointX, z - middlePointZ).ceil
106 end
107 points = Set.new
108 yzraster(centeredF, points)
109 yxraster(centeredF, points)
110 modifyBlocks(cube, centeredF, fillFunction, points)
111 end
112
113
114
115 def save
116 @region.exportToFile @regionFile
117 end
118
119 protected
120 def modifyBlocks(cube, centeredF, fillFunction, points)
121 cube.each do |b, z, x, y|
122 if points.include?([z, x, y])
123 args = [b, z, x, y].take(fillFunction.arity)
124 fillFunction.call *args
125 end
126 end
127 end
128
129 def yxraster(centeredF, points)
130 raster do |z, x|
131 p1 = point(x, centeredF.call(x, z))
132 p2 = point(x, centeredF.call(x + 1, z))
133 for newx, y in draw_line p1, p2
134 points.add [z,newx, y]
135 end
136 end
137 end
138
139 def raster(&block)
140 for z in 0..width
141 for x in 0..length
142 begin
143 block.call(x, z)
144 rescue Exception => ex
145 # Ignroring infinites, complex numbers and non defined points
146 end
147 end
148 end
149 end
150
151 def yzraster(centeredF, points)
152 raster do |z, x|
153 p1 = point(z, centeredF.call(x, z))
154 p2 = point(z, centeredF.call(x, z + 1))
155 for newz, y in draw_line p1, p2
156 points.add [newz, x, y]
157 end
158 end
159 end
160
161 def getCube
162 ret = @region.cube @z, @x, @y, :width => width, :length => length, :height => height
163 @x += size
164 if @x >= 3 * size
165 @x = 0
166 @z += size
167 end
168 return ret
169 end
170
171 def padding
172 5
173 end
174 end
175
176
177 if __FILE__ == $0
178 puts "Starting"
179 reg = '/home/daniel/.minecraft/saves/LowDirt/region/r.0.0.mcr'
180 p = PlottingExample.new reg
181 for type in Plots.public_instance_methods
182 puts "Plotting #{type}"
183 p.send type
184 end
185 puts 'exporting'
186 p.save
187 puts 'done'
188 end
Something went wrong with that request. Please try again.