0

I am struggling a bit with a list which is defined in a particular way. This is the situation: I have a Struct called Edge that is formed by two Points defined in the struct Point in the code (below).

My input list is called "EDGE". Using that list I would like to produce two outputs:

List POINTS : contains the non-repeated points that appear in the list "EDGE". Int[,] edgeIndices: represents the same thing as the input list "EDGE" but instead of the points, I want the indices of such points as defined in the list "POINTS" previously created. Can you please help me on this?

Please note that it is very important to use a List and a Int[,] and not other types.

Many thanks!

using System;
using System.Collections.Generic;


namespace Test
{

struct Point
{
    public readonly double X;
    public readonly double Y;

    public Point(double x, double y)
    {
        X = x;
        Y = y;
    }
}

struct Edge
{
    public Point First;
    public Point Second;

    public Edge(Point First, Point Second)
    {
        this.First = First;
        this.Second = Second;
    }
}

class Program
{

    static void Main(string[] args)
    {


        ///////////////////////////INPUT////////////////////////////////////
        var EDGES = new List<Edge>();
        EDGES.Add(new Edge(new Point(5, 50), new Point(20, 100))); //EDGE 01
        EDGES.Add(new Edge(new Point(20, 100), new Point(30, 50))); //EDGE 12
        EDGES.Add(new Edge(new Point(30, 50), new Point(10, 0))); //EDGE 23
        EDGES.Add(new Edge(new Point(5, 50), new Point(30, 50))); //EDGE 02
        EDGES.Add(new Edge(new Point(5, 50), new Point(10, 0))); //EDGE 03
        EDGES.Add(new Edge(new Point(20, 100), new Point(80, 100))); //EDGE 14
        EDGES.Add(new Edge(new Point(10, 0), new Point(80, 100))); //EDGE 34

        ///////////////////////EXPECTED OUTPUTS/////////////////////////////
        ///
        ///POINTS (EXPECTED) as List<double[]>
        ///Index    X   Y
        ///  0      5   50
        ///  1      20  100
        ///  2      30  50
        ///  3      10  0
        ///  4      80  100
        ///
        ///MULTIARRAY (EXPECTED)
        ///static int[,] edgeIndices =
        ///    {
        ///        {0, 1}, {1, 2}, {2, 3}, {0, 2},
        ///        {0, 3}, {1, 4}, {3, 4}
        ///    };


    }

}
}
3
  • It looks like he is adding the same points but trully he is not. Because references are diffrent. Commented Jun 11, 2015 at 9:41
  • Fsacer15, I have checked my example again and don't see where the mistake could be. Maybe I don't get what you mean... Commented Jun 11, 2015 at 9:58
  • Yeah probaby, this is a bit harder because you want the diffrent references to represent the same point. I hope it's clearer. Commented Jun 11, 2015 at 10:02

2 Answers 2

1

I suggest you to store distinct point values in List(). You can use following code to achieve it:

using System;
using System.Collections.Generic;

namespace Test
{
struct Point
{
    public readonly double X;
    public readonly double Y;

    public Point(double x, double y)
    {
        X = x;
        Y = y;
    }
}

struct Edge
{
    public Point First;
    public Point Second;

    public Edge(Point First, Point Second)
    {
        this.First = First;
        this.Second = Second;
    }
}

class Program
{

    static void Main(string[] args)
    {
        ///////////////////////////INPUT////////////////////////////////////
        var EDGES = new List<Edge>();
        EDGES.Add(new Edge(new Point(5, 50), new Point(20, 100))); //EDGE 01
        EDGES.Add(new Edge(new Point(20, 100), new Point(30, 50))); //EDGE 12
        EDGES.Add(new Edge(new Point(30, 50), new Point(10, 0))); //EDGE 23
        EDGES.Add(new Edge(new Point(5, 50), new Point(30, 50))); //EDGE 02
        EDGES.Add(new Edge(new Point(5, 50), new Point(10, 0))); //EDGE 03
        EDGES.Add(new Edge(new Point(20, 100), new Point(80, 100))); //EDGE 14
        EDGES.Add(new Edge(new Point(10, 0), new Point(80, 100))); //EDGE 34

        //FLL POINTS CACHE
        var distinctPoints = new List<Point>();
        foreach (Edge edge in EDGES)
        {
            if (!distinctPoints.Contains(edge.First))
                distinctPoints.Add(edge.First);
            if (!distinctPoints.Contains(edge.Second))
                distinctPoints.Add(edge.Second);
        }

        //POINTS LIST OUTPUT
        for (int i = 0; i < distinctPoints.Count; i++)
        {
            Console.WriteLine("{0} {1} {2}", i, distinctPoints[i].X, distinctPoints[i].Y);
        }

        //FILL 2D ARRAY OF INDICES
        int[,] edgeIndices = new int[EDGES.Count, 2];
        for (int i = 0; i < EDGES.Count; i++)
        {
            edgeIndices[i, 0] = distinctPoints.IndexOf(EDGES[i].First);
            edgeIndices[i, 1] = distinctPoints.IndexOf(EDGES[i].Second);
        }

        //2D ARRAY OUTPUT
        for (int i = 0; i < edgeIndices.GetLength(0); i++)
        {
            Console.WriteLine("({0}, {1})", edgeIndices[i, 0], edgeIndices[i, 1]);                
        }

        Console.ReadKey();
    }

}
}

Here is my output:

enter image description here

Sign up to request clarification or add additional context in comments.

1 Comment

Nice implementation I totally forgot about IndexOf and I was implementing it more with arrays than lists.
1

Here is mine implementation:

using System;
using System.Collections.Generic;


namespace Test{

struct Point
{
    public readonly double X;
    public readonly double Y;

    public Point(double x, double y)
    {
        X = x;
        Y = y;
    }
}

struct Edge
{
    public Point First;
    public Point Second;

    public Edge(Point First, Point Second)
    {
        this.First = First;
        this.Second = Second;
    }
}

class Program
{

    static void Main(string[] args)
    {


        ///////////////////////////INPUT////////////////////////////////////
        var EDGES = new List<Edge>();
        EDGES.Add(new Edge(new Point(5, 50), new Point(20, 100))); //EDGE 01
        EDGES.Add(new Edge(new Point(20, 100), new Point(30, 50))); //EDGE 12
        EDGES.Add(new Edge(new Point(30, 50), new Point(10, 0))); //EDGE 23
        EDGES.Add(new Edge(new Point(5, 50), new Point(30, 50))); //EDGE 02
        EDGES.Add(new Edge(new Point(5, 50), new Point(10, 0))); //EDGE 03
        EDGES.Add(new Edge(new Point(20, 100), new Point(80, 100))); //EDGE 14
        EDGES.Add(new Edge(new Point(10, 0), new Point(80, 100))); //EDGE 34

        var POINTS = new List<double[]>(EDGES.Count * 2);
        FillPoints(EDGES, ref POINTS);
        for (int i = 0; i < POINTS.Count; i++)
        {
            Console.WriteLine("{0} {1} {2}", i, POINTS[i][0], POINTS[i][1]);
        }

        Console.WriteLine();

        var edgeIndices = new int[EDGES.Count, 2];
        FillEdges(EDGES, POINTS, ref edgeIndices);
        for (int i = 0; i < edgeIndices.GetLength(0); i++)
        {
            Console.WriteLine("({0}, {1})", edgeIndices[i, 0], edgeIndices[i, 1]);
        }
        Console.ReadKey(true);
    }
    static bool ListContainsPoint(List<double[]> POINTS, double[] POINT) 
    {
        bool found = false;
        for (int i = 0; i < POINTS.Count; i++)
        {
            var current = POINTS[i];
            if (current[0] == POINT[0] && current[1] == POINT[1]) 
            {
                found = true;
                break;
            }
        }
        return found;
    }
    static int FindFirst(List<double[]> POINTS, double[] POINT) 
    {
        int index = -1;
        for (int i = 0; i < POINTS.Count; i++)
        {
            if (POINTS[i][0] == POINT[0] && POINTS[i][1] == POINT[1])
            {
                index = i;
                break;
            }
        }
        return index;
    }
    static void FillPoints(List<Edge> EDGES, ref List<double[]> POINTS) 
    {
        for (int i = 0; i < EDGES.Count; i++)
        {
            var current = EDGES[i];
            var firstPoint = new double[]{current.First.X, current.First.Y};
            var secondPoint = new double[]{current.Second.X, current.Second.Y};
            var firstCheck = ListContainsPoint(POINTS, firstPoint);
            var secondCheck = ListContainsPoint(POINTS, secondPoint);
            if (!firstCheck) POINTS.Add(firstPoint);
            if (!secondCheck) POINTS.Add(secondPoint);
        }
    }
    static void FillEdges(List<Edge> EDGES, List<double[]> POINTS, ref int[,] edgeIndices) 
    {
        for (int i = 0; i < EDGES.Count; i++) 
        {
            edgeIndices[i, 0] = FindFirst(POINTS, new double[] { EDGES[i].First.X, EDGES[i].First.Y });
            edgeIndices[i, 1] = FindFirst(POINTS, new double[] { EDGES[i].Second.X, EDGES[i].Second.Y });
        }
    }
}

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.