From cbea6938a6643f7537be8c245b5aac363f354446 Mon Sep 17 00:00:00 2001 From: Paul-Winpenny <92634321+Paul-Winpenny@users.noreply.github.com> Date: Wed, 8 Jan 2025 14:42:54 +0000 Subject: [PATCH] Nodes are now in correct places for desktop. --- App/RobobinApp/Views/CentreGraph.xaml.cs | 48 +++++++++++++++++++++--- 1 file changed, 42 insertions(+), 6 deletions(-) diff --git a/App/RobobinApp/Views/CentreGraph.xaml.cs b/App/RobobinApp/Views/CentreGraph.xaml.cs index d27595ea..43cf7946 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; } + } } -- GitLab