Spiral.cs 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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. */
  28. using System;
  29. using System.Collections.Generic;
  30. namespace libTerrain
  31. {
  32. partial class Channel
  33. {
  34. private double[] CoordinatesToPolar(int x, int y)
  35. {
  36. double theta = Math.Atan2(x - (w/2), y - (h/2));
  37. double rx = (double) x - ((double) w/2);
  38. double ry = (double) y - ((double) h/2);
  39. double r = Math.Sqrt((rx*rx) + (ry*ry));
  40. double[] coords = new double[2];
  41. coords[0] = r;
  42. coords[1] = theta;
  43. return coords;
  44. }
  45. public int[] PolarToCoordinates(double r, double theta)
  46. {
  47. double nx;
  48. double ny;
  49. nx = (double) r*Math.Cos(theta);
  50. ny = (double) r*Math.Sin(theta);
  51. nx += w/2;
  52. ny += h/2;
  53. if (nx >= w)
  54. nx = w - 1;
  55. if (ny >= h)
  56. ny = h - 1;
  57. if (nx < 0)
  58. nx = 0;
  59. if (ny < 0)
  60. ny = 0;
  61. int[] coords = new int[2];
  62. coords[0] = (int) nx;
  63. coords[1] = (int) ny;
  64. return coords;
  65. }
  66. public void Polar()
  67. {
  68. SetDiff();
  69. Channel n = Copy();
  70. int x, y;
  71. for (x = 0; x < w; x++)
  72. {
  73. for (y = 0; y < h; y++)
  74. {
  75. double[] coords = CoordinatesToPolar(x, y);
  76. coords[0] += w/2.0;
  77. coords[1] += h/2.0;
  78. map[x, y] = n.map[(int) coords[0]%n.w, (int) coords[1]%n.h];
  79. }
  80. }
  81. }
  82. public void SpiralPlanter(int steps, double incAngle, double incRadius, double offsetRadius, double offsetAngle)
  83. {
  84. SetDiff();
  85. int i;
  86. double r = offsetRadius;
  87. double theta = offsetAngle;
  88. for (i = 0; i < steps; i++)
  89. {
  90. r += incRadius;
  91. theta += incAngle;
  92. int[] coords = PolarToCoordinates(r, theta);
  93. Raise(coords[0], coords[1], 20, 1);
  94. }
  95. }
  96. public void SpiralCells(int steps, double incAngle, double incRadius, double offsetRadius, double offsetAngle,
  97. double[] c)
  98. {
  99. SetDiff();
  100. List<Point2D> points = new List<Point2D>();
  101. int i;
  102. double r = offsetRadius;
  103. double theta = offsetAngle;
  104. for (i = 0; i < steps; i++)
  105. {
  106. r += incRadius;
  107. theta += incAngle;
  108. int[] coords = PolarToCoordinates(r, theta);
  109. points.Add(new Point2D(coords[0], coords[1]));
  110. }
  111. VoronoiDiagram(points, c);
  112. }
  113. public void Spiral(double wid, double hig, double offset)
  114. {
  115. SetDiff();
  116. int x, y, z;
  117. z = 0;
  118. for (x = 0; x < w; x++)
  119. {
  120. for (y = 0; y < h; y++)
  121. {
  122. z++;
  123. double dx = Math.Abs((w/2) - x);
  124. double dy = Math.Abs((h/2) - y);
  125. map[x, y] += Math.Sin(dx/wid) + Math.Cos(dy/hig);
  126. }
  127. }
  128. Normalise();
  129. }
  130. }
  131. }