public group() { points = new Cartesian[defaultNumberOfPoints]; for (int i = 0; i < defaultNumberOfPoints; i++) { points[i] = new Cartesian(-1,-1); } edge1 = new Cartesian(-1, -1); edge2 = new Cartesian(-1, -1); }
public void setEdges(Cartesian point) { if (n == 0) { edge1.set(point); n++; } else if (n == 1) { edge2.set(point); } }
private void drawRoad(Cartesian pos1, Cartesian pos2) { double m = (pos2.y - pos2.y) / (pos2.x - pos1.x); double j; for (int i = pos1.x; i < pos2.x; i++) { j = m * i + pos1.y; frontier_pic.SetPixel(i, (int)j, Color.Red); } }
private void nextImage_button_Click(object sender, EventArgs e) { Cartesian[] frontier; group[] frontierGroups; numberOfFrontiers = 0; numberOfGroups = 0; #region Open A Picture OpenFileDialog ofd = new OpenFileDialog(); ofd.Title = "Select next image file."; ofd.Filter = "Png Images(*.png)|*.png|Jpeg Images(*.jpg)|*.jpg"; ofd.Filter += "|Bitmap Images(*.bmp)|*.bmp"; if (ofd.ShowDialog() == System.Windows.Forms.DialogResult.OK) { StreamReader streamReader = new StreamReader(ofd.FileName); input_pic = (Bitmap)Bitmap.FromStream(streamReader.BaseStream); streamReader.Close(); } #endregion numberOfFrontiers = 0; numberOfGroups = 0; frontier = new Cartesian[defaultNumberOfFrontiers]; for (int i = 0; i < defaultNumberOfFrontiers; i++) { frontier[i] = new Cartesian(0, 0); } frontierGroups = new group[defaultNumberOfFrontiers]; for (int i = 0; i < defaultNumberOfFrontiers; i++) { frontierGroups[i] = new group(20); } Bitmap temp_input = new Bitmap(input_pic); drawRobotPosition(temp_input, pos_X, pos_Y, Color.Purple); input_pictureBox.Image = temp_input; getAllPixels(input_pic); frontier_pic = applyFilter(input_pic); frontier_pictureBox.Image = frontier_pic; bool truth = true; for (int i = 0; i < frontier_pic.Width; i++) { for (int j = 0; j < frontier_pic.Height; j++) { d[i, j] = 0; } } for (int i = 0; i < frontier_pic.Width; i++) { for (int j = 0; j < frontier_pic.Height; j++) { if (colorDetecting(frontier_pic, i, j) == 1) { d[i, j] = 1; } } } numberOfGroups = 2; do { truth = detectGroups(frontier_pic); x_queue.Clear(); y_queue.Clear(); } while (truth); #region reGroup(frontier_pic); for (int k = 2; k < numberOfGroups; k++) { for (int i = 0; i < frontier_pic.Width; i++) { for (int j = 0; j < frontier_pic.Height; j++) { if (d[i, j] == k) { frontierGroups[k - 2].setPoint(i, j); } } } } #endregion numberOfGroups = numberOfGroups - 2; #region detectFrontier(frontier_pic); for (int i = 0; i < numberOfGroups; i++) { frontier[numberOfFrontiers] = frontierGroups[i].returnRandomPoint(); drawFrontiers(frontier_pic, frontier[numberOfFrontiers].x, frontier[numberOfFrontiers].y, Color.Red); numberOfFrontiers++; } #endregion #region selectFrontier(frontier_pic); Cartesian pos = new Cartesian(pos_X, pos_Y); double distance = 5000; Cartesian temp = new Cartesian(-1, -1); int index = -1; for (int i = 0; i < numberOfFrontiers; i++) { if (!frontier[i].check_parameter) { if (distance > pos.distanceCalculator(frontier[i])) { distance = pos.distanceCalculator(frontier[i]); temp.x = frontier[i].x; temp.y = frontier[i].y; index = i; } } } if (temp.x == -1 || temp.y == -1) MessageBox.Show("Finish"); else { pos_X = temp.x; pos_Y = temp.y; frontier[index].check(); drawFrontiers(frontier_pic, pos_X, pos_Y, Color.Blue); } #endregion }
private bool detectGroups(Bitmap pic) { Cartesian resource = new Cartesian(); Cartesian temp = new Cartesian(); resource = detectFirstWhitePoint(pic); if ((resource.x < 0) || (resource.y < 0)) { return false; } x_queue.Enqueue(resource.x); y_queue.Enqueue(resource.y); while (x_queue.Count > 0 && y_queue.Count > 0) { temp.x = x_queue.Dequeue(); temp.y = y_queue.Dequeue(); d[temp.x, temp.y] = numberOfGroups; for (int i = -1; i <= 1; i++) { for (int j = -1; j <= 1; j++) { if (d[temp.x + i, temp.y + j] == 1) { x_queue.Enqueue(temp.x + i); y_queue.Enqueue(temp.y + j); } } } } numberOfGroups++; return true; }
private Cartesian detectFirstWhitePoint(Bitmap pic) { Cartesian output = new Cartesian(-1, -1); bool check = false; for (int i = 0; i < pic.Width; i++) { for (int j = 0; j < pic.Height; j++) { if (d[i,j] == 1) /*if (colorDetecting(pic, i, j) == 1)*/ { output.x = i; output.y = j; check = true; break; } } if (check) break; } return output; }
public void setPoint(Cartesian input) { points[numberOfPoints].set(input); numberOfPoints++; }
public void set(Cartesian input) { x = input.x; y = input.y; }
public double distanceCalculator(Cartesian input) { double sum = Math.Pow((this.x - input.x), 2) + Math.Pow((this.y - input.y), 2); return Math.Sqrt(sum); }