BalthazarArgall
BalthazarArgall
CC#
Created by BalthazarArgall on 12/9/2024 in #help
Verbosity to the point of unusability in generic graph class
I am trying to create a graph class, I'm using a lot of generics, it works fine but adding a weighted edge of weighted vertices is extremely unwieldy and basically unreadable.
weightedGraph.AddEdge(new WeightedEdge<Foo, WeightedVertex<Foo, int>, int>(new WeightedVertex<Foo, int>(new Foo(), 0), new WeightedVertex<Foo, int>(new Foo(), 0), 0));
weightedGraph.AddEdge(new WeightedEdge<Foo, WeightedVertex<Foo, int>, int>(new WeightedVertex<Foo, int>(new Foo(), 0), new WeightedVertex<Foo, int>(new Foo(), 0), 0));
This is insanely verbose but I have no idea how I could improve it without sacrificing type safety or versatility. For reference, the weighted edge and vertex classes:
public class WeightedVertex<T, TWeight> : IVertex<T>
{
public T Value { get; set; }
public TWeight Weight { get; set; }
public WeightedVertex(T value, TWeight weight)
{
Value = value;
Weight = weight;
}
}
public class WeightedEdge<T, TVertex, TWeight> : IEdge<T, TVertex> where TVertex : IVertex<T>
{
public TVertex FirstVertex { get; set; }
public TVertex SecondVertex { get; set; }
public TWeight Weight { get; set; }

public WeightedEdge(TVertex firstVertex, TVertex secondVertex, TWeight weight)
{
FirstVertex = firstVertex;
SecondVertex = secondVertex;
Weight = weight;
}
}
public class WeightedVertex<T, TWeight> : IVertex<T>
{
public T Value { get; set; }
public TWeight Weight { get; set; }
public WeightedVertex(T value, TWeight weight)
{
Value = value;
Weight = weight;
}
}
public class WeightedEdge<T, TVertex, TWeight> : IEdge<T, TVertex> where TVertex : IVertex<T>
{
public TVertex FirstVertex { get; set; }
public TVertex SecondVertex { get; set; }
public TWeight Weight { get; set; }

public WeightedEdge(TVertex firstVertex, TVertex secondVertex, TWeight weight)
{
FirstVertex = firstVertex;
SecondVertex = secondVertex;
Weight = weight;
}
}
and the graph class definition for a directed graph
public class DirectedGraph<T, TVertex, TEdge> where TVertex : IVertex<T> where TEdge : IEdge<T, TVertex> {}
public class DirectedGraph<T, TVertex, TEdge> where TVertex : IVertex<T> where TEdge : IEdge<T, TVertex> {}
50 replies
CC#
Created by BalthazarArgall on 9/4/2024 in #help
How do I reference either the x or y field of a Vector2 depending on some condition?
Hello. I'm resizing a grid, to do so I use two functions, the first resizes the grid in the X axis, the second on the Y axis. I would like to merge them but I have difficulties coming up with a solution. My main issue is that I would need to have some way to tell the method to either use (and update, very important) the x or y field of a Vector2 depending on some boolean (if changeXAxis = true then use coordinates.x else use coordinates.y, if it makes sense). I have tried multiple things, for example setting variables with a ternary operator like so:
var (directionChange, coordinateX, coordinateY, targetSizeX, currentSizeY)
= modifyAxisX
? (directionChangeX, coordinates.x, coordinates.y, targetSize.x, currentSize.y)
: (directionChangeY, coordinates.y, coordinates.x, targetSize.y, currentSize.x);
var (directionChange, coordinateX, coordinateY, targetSizeX, currentSizeY)
= modifyAxisX
? (directionChangeX, coordinates.x, coordinates.y, targetSize.x, currentSize.y)
: (directionChangeY, coordinates.y, coordinates.x, targetSize.y, currentSize.x);
But then coordinateX become it's own variable, I have tried using the ref keyword in different ways but couldn't get it to make this work. Is there some concept I'm not aware of that would solve my issue or am I stuck with two methods that do basically the same thing? On a side note, I know I should put the ModifyGrid() outside of the UpdateTiles() one but this is very much a work in progress still. I'm putting my current method below because it's just slightly too big to comfortably fit in an image. The other is the same, just inverted basically.
void ModifyGridX()
{
for (coordinates.y = 0; coordinates.y < currentSize.y; coordinates.y++)
{
coordinates.x = currentSize.x - 1;
while (coordinates.x + 1 != targetSize.x)
{
if (directionChangeX == -1)
{
DestroyTile(coordinates);
coordinates.x += directionChangeX;
}
else
{
coordinates.x += directionChangeX;
CreateTile(coordinates);
}
}
}
currentSize.x = targetSize.x;
}
void ModifyGridX()
{
for (coordinates.y = 0; coordinates.y < currentSize.y; coordinates.y++)
{
coordinates.x = currentSize.x - 1;
while (coordinates.x + 1 != targetSize.x)
{
if (directionChangeX == -1)
{
DestroyTile(coordinates);
coordinates.x += directionChangeX;
}
else
{
coordinates.x += directionChangeX;
CreateTile(coordinates);
}
}
}
currentSize.x = targetSize.x;
}
2 replies