Module: Bresenham::Circle

Defined in:
lib/bresenham/circle.rb

Class Method Summary collapse

Class Method Details

.coordinates(xm, ym, radius) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/bresenham/circle.rb', line 3

def self.coordinates(xm, ym, radius)
  x = -radius
  y = 0
  err = 2-2*radius
  coords = Set.new

  begin
    coords << [xm + x, ym - y]
    coords << [xm - x, ym + y]
    coords << [xm + y, ym + x]
    coords << [xm - y, ym - x]
    radius = err
    if radius <= y
      y += 1
      err += y * 2 + 1
    end
    if radius > x or err > y
      x += 1
      err += x * 2 + 1
    end
  end while (x < 0)
  coords
end

.coordinates2(x0, y0, r) ⇒ Object



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
# File 'lib/bresenham/circle.rb', line 27

def self.coordinates2(x0, y0, r)
  p = 3 - 2 * r
  x = 0
  y = r
  coords = Set.new

  while (y >= x)
    coords << [x0 + x, y0 + y]
    coords << [x0 + x, y0 - y]
    coords << [x0 - x, y0 + y]
    coords << [x0 - x, y0 - y]
    coords << [x0 + y, y0 + x]
    coords << [x0 + y, y0 - x]
    coords << [x0 - y, y0 + x]
    coords << [x0 - y, y0 - x]
    if (p < 0)
      p += 4*x + 6
      x += 1
    else
      p += 4*(x - y) + 10
      x += 1
      y -= 1
    end
  end
  coords
end

.midpoint_algorithm(x0, y0, radius) ⇒ Object



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
# File 'lib/bresenham/circle.rb', line 54

def self.midpoint_algorithm(x0, y0, radius)
  f = 1 - radius
  ddF_x = 1
  ddF_y = -2 * radius
  x = 0
  y = radius
  coords = Set.new

  coords << [x0, y0 + radius]
  coords << [x0, y0 - radius]
  coords << [x0 + radius, y0]
  coords << [x0 - radius, y0]

  while x < y
    if f >= 0
      y -= 1
      ddF_y += 2
      f += ddF_y
    end
    x += 1
    ddF_x += 2;
    f += ddF_x;    
    coords << [x0 + x, y0 + y]
    coords << [x0 - x, y0 + y]
    coords << [x0 + x, y0 - y]
    coords << [x0 - x, y0 - y]
    coords << [x0 + y, y0 + x]
    coords << [x0 - y, y0 + x]
    coords << [x0 + y, y0 - x]
    coords << [x0 - y, y0 - x]
  end
  coords
end