1
0

Spiral.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. /*
  2. * Copyright (c) Contributors, http://opensimulator.org/
  3. * See CONTRIBUTORS.TXT for a full list of copyright holders.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. * * Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * * Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. * * Neither the name of the OpenSim Project nor the
  13. * names of its contributors may be used to endorse or promote products
  14. * derived from this software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY
  17. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  18. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  19. * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY
  20. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  21. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  22. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  23. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  24. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  25. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. using System;
  28. using System.Collections.Generic;
  29. namespace libTerrain
  30. {
  31. partial class Channel
  32. {
  33. private double[] CoordinatesToPolar(int x, int y)
  34. {
  35. double theta = Math.Atan2(x - (w/2), y - (h/2));
  36. double rx = (double) x - ((double) w/2);
  37. double ry = (double) y - ((double) h/2);
  38. double r = Math.Sqrt((rx*rx) + (ry*ry));
  39. double[] coords = new double[2];
  40. coords[0] = r;
  41. coords[1] = theta;
  42. return coords;
  43. }
  44. public int[] PolarToCoordinates(double r, double theta)
  45. {
  46. double nx;
  47. double ny;
  48. nx = (double) r*Math.Cos(theta);
  49. ny = (double) r*Math.Sin(theta);
  50. nx += w/2;
  51. ny += h/2;
  52. if (nx >= w)
  53. nx = w - 1;
  54. if (ny >= h)
  55. ny = h - 1;
  56. if (nx < 0)
  57. nx = 0;
  58. if (ny < 0)
  59. ny = 0;
  60. int[] coords = new int[2];
  61. coords[0] = (int) nx;
  62. coords[1] = (int) ny;
  63. return coords;
  64. }
  65. public void Polar()
  66. {
  67. SetDiff();
  68. Channel n = Copy();
  69. int x, y;
  70. for (x = 0; x < w; x++)
  71. {
  72. for (y = 0; y < h; y++)
  73. {
  74. double[] coords = CoordinatesToPolar(x, y);
  75. coords[0] += w/2.0;
  76. coords[1] += h/2.0;
  77. map[x, y] = n.map[(int) coords[0]%n.w, (int) coords[1]%n.h];
  78. }
  79. }
  80. }
  81. public void SpiralPlanter(int steps, double incAngle, double incRadius, double offsetRadius, double offsetAngle)
  82. {
  83. SetDiff();
  84. int i;
  85. double r = offsetRadius;
  86. double theta = offsetAngle;
  87. for (i = 0; i < steps; i++)
  88. {
  89. r += incRadius;
  90. theta += incAngle;
  91. int[] coords = PolarToCoordinates(r, theta);
  92. Raise(coords[0], coords[1], 20, 1);
  93. }
  94. }
  95. public void SpiralCells(int steps, double incAngle, double incRadius, double offsetRadius, double offsetAngle,
  96. double[] c)
  97. {
  98. SetDiff();
  99. List<Point2D> points = new List<Point2D>();
  100. int i;
  101. double r = offsetRadius;
  102. double theta = offsetAngle;
  103. for (i = 0; i < steps; i++)
  104. {
  105. r += incRadius;
  106. theta += incAngle;
  107. int[] coords = PolarToCoordinates(r, theta);
  108. points.Add(new Point2D(coords[0], coords[1]));
  109. }
  110. VoronoiDiagram(points, c);
  111. }
  112. public void Spiral(double wid, double hig, double offset)
  113. {
  114. SetDiff();
  115. int x, y, z;
  116. z = 0;
  117. for (x = 0; x < w; x++)
  118. {
  119. for (y = 0; y < h; y++)
  120. {
  121. z++;
  122. double dx = Math.Abs((w/2) - x);
  123. double dy = Math.Abs((h/2) - y);
  124. map[x, y] += Math.Sin(dx/wid) + Math.Cos(dy/hig);
  125. }
  126. }
  127. Normalise();
  128. }
  129. }
  130. }