diff --git a/App/RobobinApp/Views/CentreGraph.xaml.cs b/App/RobobinApp/Views/CentreGraph.xaml.cs index d27595eaf9e4ea535aef49211e8b3b016191e390..43cf7946362f3b3aad18e02f3d7ee389ab668b43 100644 --- a/App/RobobinApp/Views/CentreGraph.xaml.cs +++ b/App/RobobinApp/Views/CentreGraph.xaml.cs @@ -69,31 +69,66 @@ namespace RobobinApp.Views private void DrawNodes(ICanvas canvas, float nodeRadius, Node highlightedNode) { + // Calculate offsets to center the nodes + float canvasCenterX = storedDirtyRect.Width * 4.5f / 7; + float canvasCenterY = storedDirtyRect.Height / 2; + + // Calculate the center of the graph based on node coordinates + float graphCenterX = (_graph.Nodes.Min(node => node.X) + _graph.Nodes.Max(node => node.X)) / 2; + float graphCenterY = (_graph.Nodes.Min(node => node.Y) + _graph.Nodes.Max(node => node.Y)) / 2; + + // Offset to center the graph + float offsetX = canvasCenterX - graphCenterX; + float offsetY = canvasCenterY - graphCenterY; + foreach (var node in _graph.Nodes) { - float x = node.X; // Use the original X-coordinate - float y = node.Y; // Use the original Y-coordinate + // Apply centering offsets + float centeredX = node.X + offsetX; + float centeredY = node.Y + offsetY; // Set fill color based on whether the node is highlighted canvas.FillColor = (highlightedNode == node) ? HighlightedNodeColor : DefaultNodeColor; // Draw the node as a circle - canvas.FillCircle(x, y, nodeRadius); + canvas.FillCircle(centeredX, centeredY, nodeRadius); // Optionally, draw node name or other details - canvas.FontColor = Colors.White; - canvas.DrawString(node.Name, x + nodeRadius + 2, y - nodeRadius, HorizontalAlignment.Left); + canvas.FontColor = Colors.Black; + canvas.DrawString(node.Name, centeredX + nodeRadius + 2, centeredY - nodeRadius, HorizontalAlignment.Left); } } + public Node FindNearestNode(float tapPointX, float tapPointY) { + // Calculate offsets to center the nodes + float canvasCenterX = storedDirtyRect.Width * 4.5f / 7; + float canvasCenterY = storedDirtyRect.Height / 2; + + // Calculate the center of the graph based on node coordinates + float graphCenterX = (_graph.Nodes.Min(node => node.X) + _graph.Nodes.Max(node => node.X)) / 2; + float graphCenterY = (_graph.Nodes.Min(node => node.Y) + _graph.Nodes.Max(node => node.Y)) / 2; + + // Offset to center the graph + float offsetX = canvasCenterX - graphCenterX; + float offsetY = canvasCenterY - graphCenterY; + float minDistance = float.MaxValue; Node nearestNode = null; foreach (var node in _graph.Nodes) { - float distance = (float)Math.Sqrt(Math.Pow(node.X - tapPointX, 2) + Math.Pow(node.Y - tapPointY, 2)); + // Apply centering offsets to node positions + float centeredX = node.X + offsetX; + float centeredY = node.Y + offsetY; + + // Calculate the distance between the tapped point and the centered node position + float distance = (float)Math.Sqrt( + Math.Pow(centeredX - tapPointX, 2) + + Math.Pow(centeredY - tapPointY, 2) + ); + if (distance < minDistance) { minDistance = distance; @@ -103,5 +138,6 @@ namespace RobobinApp.Views return nearestNode; } + } }