Skip to content
Snippets Groups Projects
Commit b6bc2d59 authored by Paul-Winpenny's avatar Paul-Winpenny
Browse files

Graph now displays on screen (and algorithm for polar coordinates).

parent 653179fb
No related tags found
No related merge requests found
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Robobin.Models
{
public class Node
{
public string Name { get; set; }
public float X { get; set; }
public float Y { get; set; }
public Node(string name, float x, float y)
{
Name = name;
X = x;
Y = y;
}
}
public class Graph
{
public List<Node> Nodes { get; set; }
public (float magnitude, float angle)[,] AdjacencyMatrix { get; set; }
public Graph(List<Node> nodes, (float, float)[,] adjacencyMatrix)
{
Nodes = nodes;
AdjacencyMatrix = adjacencyMatrix;
}
}
}
...@@ -2,6 +2,7 @@ using System.ComponentModel; ...@@ -2,6 +2,7 @@ using System.ComponentModel;
using System.Runtime.CompilerServices; using System.Runtime.CompilerServices;
using System.Windows.Input; using System.Windows.Input;
using Microsoft.Maui.Controls; using Microsoft.Maui.Controls;
using Robobin.Models;
using RobobinApp.Views; using RobobinApp.Views;
using Shiny.BluetoothLE; using Shiny.BluetoothLE;
...@@ -13,15 +14,62 @@ namespace RobobinApp.ViewModels ...@@ -13,15 +14,62 @@ namespace RobobinApp.ViewModels
{ {
private bool _isBusy; private bool _isBusy;
private string _statusMessage; private string _statusMessage;
public Graph Graph { get; private set; }
public ICommand ConnectToRobobinCommand { get; } public ICommand ConnectToRobobinCommand { get; }
public MainPageViewModel() public MainPageViewModel()
{ {
ConnectToRobobinCommand = new Command(async () => await OnConnectToRobobin()); ConnectToRobobinCommand = new Command(async () => await OnConnectToRobobin());
// Create nodes with X and Y coordinates
var nodes = new List<Node>
{
new Node("N1", 100, 100), // Example coordinates
new Node("N2", 200, 100),
new Node("N3", 150, 200),
};
// Create an empty adjacency matrix for three nodes
var adjacencyMatrix = new (float, float)[,]
{
{ (0, 0), (100, 0), (111.80f, 63.44f) },
{ (100, 180), (0, 0), (111.80f, 116.57f) },
{ (111.80f, -116.56f), (111.80f, -63.43f), (0, 0) }
};
// Create graph
Graph = new Graph(nodes, adjacencyMatrix);
}
public static (float magnitude, float angle)[,] CalculatePolarConnections(List<Node> nodes, bool[,] connections)
{
int nodeCount = nodes.Count;
var polarMatrix = new (float magnitude, float angle)[nodeCount, nodeCount];
for (int i = 0; i < nodeCount; i++)
{
for (int j = 0; j < nodeCount; j++)
{
if (i == j || !connections[i, j])
{
polarMatrix[i, j] = (0, 0);
continue;
}
float dx = nodes[j].X - nodes[i].X;
float dy = nodes[j].Y - nodes[i].Y;
float magnitude = (float)Math.Sqrt(dx * dx + dy * dy);
float angle = (float)(Math.Atan2(dy, dx) * (180 / Math.PI));
polarMatrix[i, j] = (magnitude, angle);
}
} }
return polarMatrix;
}
public bool IsBusy public bool IsBusy
{ {
get => _isBusy; get => _isBusy;
......
using Microsoft.Maui.Graphics; using Microsoft.Maui.Graphics;
using Robobin.Models;
using RobobinApp.Models; // Ensure you have the correct namespace for your Models
namespace RobobinApp.Views namespace RobobinApp.Views
{ {
public class GraphicsDrawable : IDrawable public class GraphicsDrawable : IDrawable
{ {
private Graph _graph;
// Method to set the graph data
public void SetGraph(Graph graph)
{
_graph = graph;
}
public void Draw(ICanvas canvas, RectF dirtyRect) public void Draw(ICanvas canvas, RectF dirtyRect)
{ {
if (_graph == null) return;
// Set the background color // Set the background color
canvas.FillColor = Colors.LightPink; // Change to your desired color canvas.FillColor = Colors.LightPink;
canvas.FillRectangle(dirtyRect); canvas.FillRectangle(dirtyRect);
// Draw some graphics (for example, a circle) // Draw nodes and edges
const float nodeRadius = 10; // Radius for nodes
foreach (var node in _graph.Nodes)
{
// Draw the node
canvas.FillColor = Colors.Red; canvas.FillColor = Colors.Red;
canvas.FillCircle(dirtyRect.Center.X, dirtyRect.Center.Y, 50); canvas.FillCircle(node.X, node.Y, nodeRadius);
canvas.DrawString(node.Name, node.X + 12, node.Y - 5, HorizontalAlignment.Left);
// Example of drawing text // Draw edges based on the adjacency matrix
canvas.FontSize = 24; for (int j = 0; j < _graph.Nodes.Count; j++)
canvas.FillColor = Colors.Black; {
canvas.DrawString("Hello, GraphicsView!", dirtyRect.Center.X, dirtyRect.Center.Y, HorizontalAlignment.Center); var (magnitude, angle) = _graph.AdjacencyMatrix[_graph.Nodes.IndexOf(node), j];
// Only draw if there's a valid connection (magnitude > 0)
if (magnitude > 0)
{
// Calculate the target node's coordinates based on the angle and magnitude
float targetX = node.X + magnitude * MathF.Cos(MathF.PI * (angle / 180));
float targetY = node.Y + magnitude * MathF.Sin(MathF.PI * (angle / 180));
// Draw edge
canvas.StrokeColor = Colors.Black;
canvas.StrokeSize = 2;
canvas.DrawLine(node.X, node.Y, targetX, targetY); // Draw edge
}
}
}
} }
} }
} }
...@@ -36,6 +36,7 @@ ...@@ -36,6 +36,7 @@
<Button Text="Setup" Command="{Binding ConnectToRobobinCommand}"/> <Button Text="Setup" Command="{Binding ConnectToRobobinCommand}"/>
</HorizontalStackLayout> </HorizontalStackLayout>
<local:SideBox HeaderTitle="Queue:" />
<local:SideBox HeaderTitle="Status:" /> <local:SideBox HeaderTitle="Status:" />
</VerticalStackLayout> </VerticalStackLayout>
...@@ -56,6 +57,8 @@ ...@@ -56,6 +57,8 @@
<Button Text="Info" /> <Button Text="Info" />
</HorizontalStackLayout> </HorizontalStackLayout>
<local:SideBox HeaderTitle="Mode:" /> <local:SideBox HeaderTitle="Mode:" />
<local:SideBox HeaderTitle="Admin:" />
</VerticalStackLayout> </VerticalStackLayout>
</Grid> </Grid>
</ContentPage> </ContentPage>
using Microsoft.Maui.Controls; using Microsoft.Maui.Controls;
using Microsoft.Extensions.DependencyInjection; using RobobinApp.ViewModels;
using RobobinApp.Models;
namespace RobobinApp.Views namespace RobobinApp.Views
{ {
...@@ -8,6 +9,15 @@ namespace RobobinApp.Views ...@@ -8,6 +9,15 @@ namespace RobobinApp.Views
public MainPage() public MainPage()
{ {
InitializeComponent(); InitializeComponent();
// Get the drawable from resources
var drawable = (GraphicsDrawable)Resources["drawable"];
// Set the graph in the drawable using the existing BindingContext
if (BindingContext is MainPageViewModel viewModel)
{
drawable.SetGraph(viewModel.Graph);
}
} }
} }
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment