private int getDistance(Node node1, Node node2) { int xDist = Math.Abs(node1.getX() - node2.getX()); int yDist = Math.Abs(node1.getY() - node2.getY()); return((int)Math.Sqrt(Math.Pow(xDist, 2) + Math.Pow(yDist, 2))); }
private bool isIncluded(List <Node> nodeList, Node testNode)//to check whether the clicked node is within nodeList { foreach (Node node in nodeList) { if (Math.Abs(node.getX() - testNode.getX()) < 5 && Math.Abs(node.getY() - testNode.getY()) < 5) //if (node.getX() == testNode.getX() && node.getY() == testNode.getY()) {//as long as the horizontal and vertical deifference is less than 3, we consider as the same point if (node1 == null) { node1 = node;//copy the actual reference of the node in the nodeList } else if (node1 != null && node2 == null) { node2 = node; } return(true); } } return(false); }
private void setNeighbors(Node node1, Node node2) { if (!isNeighbors(node1, node2)) { node1.addNeighbor(node2); node2.addNeighbor(node1); //add a line between two points Edge edge = new Edge(node1, node2); x1Array.Add(node1.getX()); y1Array.Add(node1.getY()); x2Array.Add(node2.getX()); y2Array.Add(node2.getY()); x1Index.Add(node1); x2Index.Add(node2); edge.id = x1Array.Count - 1; edgeList.Add(edge); LinePlot(false); } }
private int getDistance(Node node1, Node node2) { int xDist = Math.Abs(node1.getX() - node2.getX()); int yDist = Math.Abs(node1.getY() - node2.getY()); return (int)Math.Sqrt(Math.Pow(xDist, 2) + Math.Pow(yDist, 2)); }
private bool removeEdge(Node node) { double x = node.getX(); double y = node.getY(); for (int i = 0; i < x1Array.Count; i++) { Edge edge = edgeList[i]; Node node1 = edge.node1; Node node2 = edge.node2; //Node node1 = nodeList[int.Parse(x1Index[i].ToString())]; //Node node2 = nodeList[int.Parse(x2Index[i].ToString())]; double x1 = double.Parse(x1Array[i].ToString()); double y1 = double.Parse(y1Array[i].ToString()); double x2 = double.Parse(x2Array[i].ToString()); double y2 = double.Parse(y2Array[i].ToString()); if (x1 == x || x2 == x)//check whether they have the same x value { if (Math.Abs(x1 - x) <= EDGE_PRECISION && Math.Abs(x2 - x) <= EDGE_PRECISION && (y - y1)*(y - y2) < 0)//if 3 points are almost in line and y is in the middle { x1Array.RemoveAt(i); y1Array.RemoveAt(i); x2Array.RemoveAt(i); y2Array.RemoveAt(i); x1Index.RemoveAt(i); x2Index.RemoveAt(i); btnLoadConf.Text = "[1]," + node1.getId() + ", " + node2.getId(); node1.removeNeighbor(node2); node2.removeNeighbor(node1); edgeList.Remove(edge); LinePlot(true); PointPlot(false, pointListBox.SelectedIndex); return true; } } else if (y1 == y || y2 == y ) { if (Math.Abs(y1 - y) <= EDGE_PRECISION && Math.Abs(y2 - y) < EDGE_PRECISION && (x - x1)*(x - x2) < 0)//if 3 points are in line and x is in the middle { x1Array.RemoveAt(i); y1Array.RemoveAt(i); x2Array.RemoveAt(i); y2Array.RemoveAt(i); x1Index.RemoveAt(i); x2Index.RemoveAt(i); btnLoadConf.Text = "[2]," + node1.getId() + ", " + node2.getId(); node1.removeNeighbor(node2); node2.removeNeighbor(node1); edgeList.Remove(edge); LinePlot(true); PointPlot(false, pointListBox.SelectedIndex); return true; } } else { double slope1 = (y1 - y) / (x1 - x); double slope2 = (y2 - y) / (x2 - x); if(Math.Abs((slope1 - slope2) / slope1) < EDGE_SLOP_PRECISION && (x - x1) * (x - x2) < 0 && (y - y1)*(y - y2) < 0) { //similar slope and clickedPoint in the middle x1Array.RemoveAt(i); y1Array.RemoveAt(i); x2Array.RemoveAt(i); y2Array.RemoveAt(i); x1Index.RemoveAt(i); x2Index.RemoveAt(i); btnLoadConf.Text = "[3]," + node1.getId() + ", " + node2.getId(); node1.removeNeighbor(node2); node2.removeNeighbor(node1); edgeList.Remove(edge); LinePlot(true); PointPlot(false, pointListBox.SelectedIndex); return true; } } } return false; }
private bool isIncluded(List<Node> nodeList, Node testNode)//to check whether the clicked node is within nodeList { foreach(Node node in nodeList) { if (Math.Abs(node.getX() - testNode.getX()) < 5 && Math.Abs(node.getY() - testNode.getY()) < 5) //if (node.getX() == testNode.getX() && node.getY() == testNode.getY()) {//as long as the horizontal and vertical deifference is less than 3, we consider as the same point if (node1 == null) { node1 = node;//copy the actual reference of the node in the nodeList } else if(node1 != null && node2 == null) { node2 = node; } return true; } } return false; }
private void pictureBox1_Click(object sender, EventArgs e) { if (mapWidth > 0 && mapHeight > 0) { Point clickPoint = pictureBox1.PointToClient(Control.MousePosition); //String myText = String.Format("x: {0}, y: {1}", clickPoint.X, clickPoint.Y); //PointLabel.Text = myText; double pictureBoxWidth = (double)pictureBox1.Size.Width; double pictureBoxHeight = (double)pictureBox1.Size.Height; double clickedX = 0, clickedY = 0; //Get the X and Y of the clicked point --zpx if ((imageWidth / imageHeight) > (pictureBoxWidth / pictureBoxHeight)) { clickedX = (clickPoint.X) / pictureBoxWidth * mapWidth; double offset = (pictureBoxHeight - pictureBoxWidth / imageWidth * imageHeight) / 2; clickedY = (clickPoint.Y - offset) / (pictureBoxHeight - offset * 2) * mapHeight; } else { clickedY = (clickPoint.Y) / pictureBoxHeight * mapHeight; double offset = (pictureBoxWidth - pictureBoxHeight / imageHeight * imageWidth) / 2; clickedX = (clickPoint.X - offset) / (pictureBoxWidth - offset * 2) * mapWidth; } //Get the rounded coordinates of the clicked point --zpx bool flag = false; int x = Convert.ToInt32(clickedX); int y = Convert.ToInt32(clickedY); Node clickedNode = new Node(x, y);//create an object of the clicked node --zpx //add more properties if there pointType is not normal --zpx if (pointType == POINT_TYPE.Elevator) { clickedNode.pointType = Node.POINT_TYPE.Elevator; if (pointTypeValueTextBox.Text != "") { int elevatorGroupNum = int.Parse(pointTypeValueTextBox.Text); clickedNode.setElevatorGroupNum(elevatorGroupNum); } } else if (pointType == POINT_TYPE.Connector) { clickedNode.pointType = Node.POINT_TYPE.Connector; clickedNode.setConnectorName(pointTypeValueTextBox.Text); } /*-----------------------------------------------*/ //Input the points if (pointConfirmed == false) { if (nodeList.Count > 0) { for (int i = 0; i < nodeList.Count; i++) { if ((x == nodeList[i].getX()) && (y == nodeList[i].getY())) { flag = true;//same point, no need to add it to the nodeList --zpx //mainLabel.Text = "You have already added this point!"; pointListBox.SelectedIndex = i; mainLabel.Text = "Id= " + i + " " + "x=" + x + " " + "y=" + y + " nodeListID: " + i; } } } if (flag == false)//different points --zpx { xArray.Add(x); yArray.Add(y); //set the property of the clickedNode --zpx nodeList.Add(clickedNode); clickedNode.setId(nodeList.IndexOf(clickedNode)); //add the item to the pointListBox pointListBox.Items.Add(clickedNode.getId() + " (" + x.ToString() + ", " + y.ToString() + ")"); pointListBox.SelectedIndex = pointListBox.Items.Count - 1; //display the node info --zpx mainLabel.Text = "Id= " + clickedNode.getId() + " " + "x=" + clickedNode.getX() + " " + "y=" + clickedNode.getY(); } } //Input the edges else { if (isIncluded(nodeList, clickedNode) && firstNode == null)//add first node { firstNode = clickedNode; mainLabel.Text = "Please click the second point of the edge."; } else if (isIncluded(nodeList, clickedNode) && firstNode != null)//add second node { secondNode = clickedNode; if (!node1.Equals(node2)) { setNeighbors(node1, node2);//node 1 and 2 are the actual nodes in nodeList refreshEdgeList();//refresh th edge list to ensure that it is synchronized with the x1Index and x2Index firstNode = null; secondNode = null; node1 = null; node2 = null; mainLabel.Text = "Edge added. Please click two points for a new edge."; } else { mainLabel.Text = "Must be connected to different nodes."; node2 = null; secondNode = null; } } else if (removeEdge(clickedNode))//check whether it is in line with any edges, if yes, delete the edge { mainLabel.Text = "Edge removed."; refreshEdgeList(); } else { mainLabel.Text = "Please click the exact position."; } } } }
private bool removeEdge(Node node) { double x = node.getX(); double y = node.getY(); for (int i = 0; i < x1Array.Count; i++) { Edge edge = edgeList[i]; Node node1 = edge.node1; Node node2 = edge.node2; //Node node1 = nodeList[int.Parse(x1Index[i].ToString())]; //Node node2 = nodeList[int.Parse(x2Index[i].ToString())]; double x1 = double.Parse(x1Array[i].ToString()); double y1 = double.Parse(y1Array[i].ToString()); double x2 = double.Parse(x2Array[i].ToString()); double y2 = double.Parse(y2Array[i].ToString()); if (x1 == x || x2 == x) //check whether they have the same x value { if (Math.Abs(x1 - x) <= EDGE_PRECISION && Math.Abs(x2 - x) <= EDGE_PRECISION && (y - y1) * (y - y2) < 0) //if 3 points are almost in line and y is in the middle { x1Array.RemoveAt(i); y1Array.RemoveAt(i); x2Array.RemoveAt(i); y2Array.RemoveAt(i); x1Index.RemoveAt(i); x2Index.RemoveAt(i); btnLoadConf.Text = "[1]," + node1.getId() + ", " + node2.getId(); node1.removeNeighbor(node2); node2.removeNeighbor(node1); edgeList.Remove(edge); LinePlot(true); PointPlot(false, pointListBox.SelectedIndex); return(true); } } else if (y1 == y || y2 == y) { if (Math.Abs(y1 - y) <= EDGE_PRECISION && Math.Abs(y2 - y) < EDGE_PRECISION && (x - x1) * (x - x2) < 0)//if 3 points are in line and x is in the middle { x1Array.RemoveAt(i); y1Array.RemoveAt(i); x2Array.RemoveAt(i); y2Array.RemoveAt(i); x1Index.RemoveAt(i); x2Index.RemoveAt(i); btnLoadConf.Text = "[2]," + node1.getId() + ", " + node2.getId(); node1.removeNeighbor(node2); node2.removeNeighbor(node1); edgeList.Remove(edge); LinePlot(true); PointPlot(false, pointListBox.SelectedIndex); return(true); } } else { double slope1 = (y1 - y) / (x1 - x); double slope2 = (y2 - y) / (x2 - x); if (Math.Abs((slope1 - slope2) / slope1) < EDGE_SLOP_PRECISION && (x - x1) * (x - x2) < 0 && (y - y1) * (y - y2) < 0) { //similar slope and clickedPoint in the middle x1Array.RemoveAt(i); y1Array.RemoveAt(i); x2Array.RemoveAt(i); y2Array.RemoveAt(i); x1Index.RemoveAt(i); x2Index.RemoveAt(i); btnLoadConf.Text = "[3]," + node1.getId() + ", " + node2.getId(); node1.removeNeighbor(node2); node2.removeNeighbor(node1); edgeList.Remove(edge); LinePlot(true); PointPlot(false, pointListBox.SelectedIndex); return(true); } } } return(false); }
private void pictureBox1_Click(object sender, EventArgs e) { if (mapWidth > 0 && mapHeight > 0) { Point clickPoint = pictureBox1.PointToClient(Control.MousePosition); //String myText = String.Format("x: {0}, y: {1}", clickPoint.X, clickPoint.Y); //PointLabel.Text = myText; double pictureBoxWidth = (double)pictureBox1.Size.Width; double pictureBoxHeight = (double)pictureBox1.Size.Height; double clickedX = 0, clickedY = 0; //Get the X and Y of the clicked point --zpx if ((imageWidth / imageHeight) > (pictureBoxWidth / pictureBoxHeight)) { clickedX = (clickPoint.X) / pictureBoxWidth * mapWidth; double offset = (pictureBoxHeight - pictureBoxWidth / imageWidth * imageHeight) / 2; clickedY = (clickPoint.Y - offset) / (pictureBoxHeight - offset * 2) * mapHeight; } else { clickedY = (clickPoint.Y) / pictureBoxHeight * mapHeight; double offset = (pictureBoxWidth - pictureBoxHeight / imageHeight * imageWidth) / 2; clickedX = (clickPoint.X - offset) / (pictureBoxWidth - offset * 2) * mapWidth; } //Get the rounded coordinates of the clicked point --zpx bool flag = false; int x = Convert.ToInt32(clickedX); int y = Convert.ToInt32(clickedY); Node clickedNode = new Node(x, y);//create an object of the clicked node --zpx //add more properties if there pointType is not normal --zpx if (pointType == POINT_TYPE.Elevator) { clickedNode.pointType = Node.POINT_TYPE.Elevator; if (pointTypeValueTextBox.Text != "") { int elevatorGroupNum = int.Parse(pointTypeValueTextBox.Text); clickedNode.setElevatorGroupNum(elevatorGroupNum); } } else if (pointType == POINT_TYPE.Connector) { clickedNode.pointType = Node.POINT_TYPE.Connector; clickedNode.setConnectorName(pointTypeValueTextBox.Text); } /*-----------------------------------------------*/ //Input the points if (pointConfirmed == false) { if (nodeList.Count > 0) { for (int i = 0; i < nodeList.Count; i++) { if ((x == nodeList[i].getX()) && (y == nodeList[i].getY())) { flag = true;//same point, no need to add it to the nodeList --zpx //mainLabel.Text = "You have already added this point!"; pointListBox.SelectedIndex = i; mainLabel.Text = "Id= " + i + " " + "x=" + x + " " + "y=" + y + " nodeListID: " + i; } } } if (flag == false)//different points --zpx { xArray.Add(x); yArray.Add(y); //set the property of the clickedNode --zpx nodeList.Add(clickedNode); clickedNode.setId(nodeList.IndexOf(clickedNode)); //add the item to the pointListBox pointListBox.Items.Add(clickedNode.getId() + " (" + x.ToString() + ", " + y.ToString() + ")"); pointListBox.SelectedIndex = pointListBox.Items.Count - 1; //display the node info --zpx mainLabel.Text = "Id= " + clickedNode.getId() + " " + "x=" + clickedNode.getX() + " " + "y=" + clickedNode.getY(); } } //Input the edges else { if (isIncluded(nodeList, clickedNode) && firstNode == null)//add first node { firstNode = clickedNode; mainLabel.Text = "Please click the second point of the edge."; } else if (isIncluded(nodeList, clickedNode) && firstNode != null)//add second node { secondNode = clickedNode; if (!node1.Equals(node2)) { setNeighbors(node1, node2); //node 1 and 2 are the actual nodes in nodeList refreshEdgeList(); //refresh th edge list to ensure that it is synchronized with the x1Index and x2Index firstNode = null; secondNode = null; node1 = null; node2 = null; mainLabel.Text = "Edge added. Please click two points for a new edge."; } else { mainLabel.Text = "Must be connected to different nodes."; node2 = null; secondNode = null; } } else if (removeEdge(clickedNode))//check whether it is in line with any edges, if yes, delete the edge { mainLabel.Text = "Edge removed."; refreshEdgeList(); } else { mainLabel.Text = "Please click the exact position."; } } } }