This project is read-only.

Getting the coordinates of the shapes and splines

Oct 12, 2011 at 2:15 PM
Edited Oct 13, 2011 at 11:04 AM

What would be the most efficient way to get the coordinates of the shapes that are drawn?

Should I change the source code so it returns the array? Or is there already a way built in and I'm not seeing it?


edit: I'm just gonna go ahead and make your functions return the arrays, but I encounter this problem.

I want your DrawLine to return the pixels array. If I call writeableBmp.Drawline(int x1, int y1, ...... colors.Black) for example, I call your first Drawline function.

Your first drawline function converts the colortype to integer, then calls the second Drawline(....... int color). That function calls a 3rd DrawLine where all the math of calculating the pixels happens.

I changed all 3 functions from void to int[]. I made the 3rd DrawLine function so that it returns Pixels to the 2nd function, the 2nd function returns pixels to the 1st and the 1st function returns the pixel array to me.

The problem is in the 3rd function, there are a lot of "return;" in it, and I get the error:  An object of a type convertible to 'int[]' is required. What does 'return;' do? Does it stop all the math from happening? Or what's the point there? Maybe if I understand what's happening there, I can change it to how I like.




I can solve it by using DrawLineDDA or DrawLineBresenham.

Oct 13, 2011 at 11:40 AM
Edited Oct 13, 2011 at 2:21 PM

I did it, and I'm posting the solution here. I have a class ControlPoint, it's almost the same as Point, but it has some extra information about the points. But that doesn't matter the logic should stay the same if you are interested in this.

I changed the DrawLineDDA. It was very easy to be honest. The things I have changed are in BOLD.

I call the function like this:

List<ControlPoint> pointslist = new List<ControlPoint>();
pointslist = writeableBmp.DrawLineDDA(P1.X, P1.Y, P2.X, P2.Y, color);


This is the function you call first

  public static List<ControlPoint> DrawLineDDA(this WriteableBitmap bmp, int x1, int y1, int x2, int y2, Color color)
         List<ControlPoint> points = new List<ControlPoint>();
         // Add one to use mul and cheap bit shift for multiplicaltion
         var a = color.A + 1;
         var col = (color.A << 24)
                  | ((byte)((color.R * a) >> 8) << 16)
                  | ((byte)((color.G * a) >> 8) << 8)
                  | ((byte)((color.B * a) >> 8));
         points = bmp.DrawLineDDA(x1, y1, x2, y2, col);
         return points;

The function above calls the function below:

public static List<ControlPoint> DrawLineDDA(this WriteableBitmap bmp, int x1, int y1, int x2, int y2, int color)
         // Use refs for faster access (really important!) speeds up a lot!
         int w = bmp.PixelWidth;
         int h = bmp.PixelHeight;
         int[] pixels = bmp.Pixels;
         List<ControlPoint> points = new List<ControlPoint>();

         // Distance start and end point
         int dx = x2 - x1;
         int dy = y2 - y1;

         // Determine slope (absoulte value)
         int len = dy >= 0 ? dy : -dy;
         int lenx = dx >= 0 ? dx : -dx;
         if (lenx > len)
            len = lenx;

         // Prevent divison by zero
         if (len != 0)
            // Init steps and start
            float incx = dx / (float)len;
            float incy = dy / (float)len;
            float x = x1;
            float y = y1;

            // Walk the line!
            for (int i = 0; i < len; i++)
               if (y < h && y >= 0 && x < w && x >= 0)
                  int arrayvalue = (int)y * w + (int)x;
                  pixels[arrayvalue] = color;
                  ControlPoint point = new ControlPoint();
                  point.X = arrayvalue % w;
                  point.Y = arrayvalue / w;
               x += incx;
               y += incy;
         return points;


I also did this with the Cardinal. It was harder to do, but also do-able. I won't copy paste it here, but I'm willing if somebody wants it.