File: procedural_primitives.lua

package info (click to toggle)
crawl 2%3A0.33.1-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 95,264 kB
  • sloc: cpp: 358,145; ansic: 27,203; javascript: 9,491; python: 8,359; perl: 3,327; java: 2,667; xml: 2,191; makefile: 1,830; sh: 611; objc: 250; cs: 15; sed: 9; lisp: 3
file content (97 lines) | stat: -rw-r--r-- 2,658 bytes parent folder | download | duplicates (5)
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
------------------------------------------------------------------------------
-- procedural_primitive.lua: Primitive procedural functions
--
-- These procedurals can be used as a base to build up more complex objects
------------------------------------------------------------------------------

-- crawl_require("dlua/layout/procedural.lua")

primitive = {}

function primitive.linear_x(x,y)
  return x
end

function primitive.linear_y(x,y)
  return y
end

function primitive.linear_xy(x,y)
  return x+y
end

function primitive.linear_yx(x,y)
  return x-y
end

function primitive.box(x,y)
  return math.max(math.abs(x),math.abs(y))
end

-- Returns distance from the origin, useful for circles and ellipses
function primitive.distance(x,y)
  return math.sqrt(math.pow(x, 2) + math.pow(y, 2))
end

-- Gives the radial value from a point, i.e. the arctangent of the line
-- from the origin to the point
function primitive.radial(x,y)
  local r
  if y == 0 then r = (x>0) and 270 or 90
  else
    r = math.atan(x/y) / math.pi * 180
    if y>0 then r = r+180 end
  end
  return (r % 360)/360
end

------------------------------------------------------------------------------
-- Slightly more complex functions
--
-- But still primitive enough to count

function primitive.cross()
  return procedural.min(procedural.abs(primitive.linear_x),procedural.abs(primitive.linear_y))
end

function primitive.ring(radius)
  return procedural.abs(procedural.sub(primitive.distance,radius))
end

function primitive.ringify(func,radius)
  return procedural.abs(procedural.sub(func,radius))
end

-- A large X-shape
function primitive.ex()
  return procedural.min(procedural.abs(primitive.linear_xy), procedural.abs(primitive.linear_yx))
end

function primitive.diamond()
  return procedural.max(procedural.abs(primitive.linear_xy), procedural.abs(primitive.linear_yx))
end

function primitive.octagon(edge)
  return procedural.max(primitive.box,procedural.sub(primitive.diamond(),edge))
end

function primitive.hexagon(edge)
  -- Use a domain transform to warp an octagon into a hexagon
  -- There must be a less hackish way to do this but I can't think of it today...
  local foct = primitive.octagon(edge)
  return function(x,y)
    return foct(x,math.abs(y)+edge)
  end
end

function primitive.triangle()
  -- This isn't a very good primitive because it's a hacked-together triangle.
  -- Should just come up with a proper algorithm at some point.
  local tri = procedural.max(primitive.linear_y,procedural.neg(primitive.linear_xy),primitive.linear_yx)
  if crawl.coinflip() then return procedural.flip_y(tri) end
  return tri
end

function primitive.spiral(phase)

end