Advertisement
Guest User

Untitled

a guest
Jul 1st, 2015
211
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.96 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Windows.Forms;
  9.  
  10. namespace WindowsFormsApplication1
  11. {
  12.  
  13. public partial class Form1 : Form
  14. {
  15. public static int X_WIDTH = 1000;
  16. public static int Y_WIDTH = 1000;
  17. public static int RADIUS_SIZE = 50;
  18.  
  19. public void RENDER(int personX, int personY)
  20. {
  21. Node[] nodes = new Node[]
  22. {
  23. new Node(1,100,100,Color.Red),
  24. new Node(2,150,100,Color.Blue),
  25. new Node(3,125,150,Color.Green)
  26.  
  27. };
  28. Bitmap bmp = new Bitmap(X_WIDTH, Y_WIDTH);
  29.  
  30.  
  31.  
  32.  
  33. for (int x = 0; x < X_WIDTH; x++)
  34. {
  35. for (int y = 0; y < Y_WIDTH; y++)
  36. {
  37.  
  38. foreach (Node n in nodes)
  39. {
  40. double deltaR = n.GetDistance(x,y);
  41. if (deltaR < RADIUS_SIZE && (deltaR > RADIUS_SIZE - 1))
  42. {
  43. bmp.SetPixel(x, y, ControlPaint.LightLight( n.Color));
  44. }
  45. }
  46. }
  47. }
  48.  
  49.  
  50. foreach (Node n in nodes)
  51. {
  52.  
  53.  
  54. bmp.SetPixel(n.Pos.X, n.Pos.Y, n.Color);
  55.  
  56. }
  57. bmp.SetPixel(personX, personY, Color.Black);
  58.  
  59. IEnumerable<Beacon> beacons = nodes.Select(n => new Beacon(n.Id, n.GetDistance(personX, personY))).Where(b => b.Distance <= 50);
  60.  
  61.  
  62. for (int x = 0; x < X_WIDTH; x++)
  63. {
  64. for (int y = 0; y < Y_WIDTH; y++)
  65. {
  66.  
  67. foreach (Beacon b in beacons)
  68. {
  69. Node n = nodes.SingleOrDefault(no => no.Id == b.Id);
  70. double deltaR = n.GetDistance(x, y);
  71. if (deltaR < b.Distance && (deltaR > b.Distance - 1))
  72. {
  73. bmp.SetPixel(x, y, ControlPaint.Dark(n.Color));
  74. }
  75. }
  76. }
  77. }
  78.  
  79. pictureBox1.Image = bmp;
  80.  
  81. Position calcPos = GetPos(beacons, b => nodes.SingleOrDefault(n => b.Id == n.Id));
  82.  
  83. MessageBox.Show(String.Format("Actual: {0},{1}\nCalc: {2},{3}", personX, personY, calcPos.X, calcPos.Y));
  84.  
  85. }
  86. public Position GetPos(IEnumerable<Beacon> beacons, Func<Beacon,Node> getNode)
  87. {
  88. Position pos = new Position(0,0);
  89.  
  90. return pos;
  91. }
  92. public Form1()
  93. {
  94. InitializeComponent();
  95. pictureBox1.MouseClick += new MouseEventHandler(pictureBox1_MouseClick);
  96. }
  97.  
  98. void pictureBox1_MouseClick(object sender, MouseEventArgs e)
  99. {
  100. RENDER(e.X, e.Y);
  101.  
  102. }
  103.  
  104. }
  105. public class Node
  106. {
  107. public Position Pos;
  108. public Color Color;
  109. public int Id;
  110. public Node(int id, int x, int y, Color color)
  111. {
  112. Id = id;
  113. Pos = new Position(x, y);
  114. Color = color;
  115. }
  116.  
  117. public double GetDistance(int x, int y)
  118. {
  119. int deltaX = Math.Abs(x - this.Pos.X);
  120. int deltaY = Math.Abs(y - this.Pos.Y);
  121.  
  122. double deltaR = Math.Sqrt(deltaX * deltaX + deltaY * deltaY);
  123.  
  124.  
  125. return deltaR;
  126. }
  127.  
  128. }
  129. public class Beacon
  130. {
  131. public int Id;
  132. public double Distance;
  133. public Beacon(int id, double distance)
  134. {
  135. Id = id;
  136. Distance = distance;
  137. }
  138. }
  139. public class Position
  140. {
  141. public int X;
  142. public int Y;
  143. public Position(int x, int y)
  144. {
  145. X = x;
  146. Y = y;
  147. }
  148. }
  149. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement