12 Replies - 481 Views - Last Post: 28 November 2013 - 03:11 PM Rate Topic: -----

#1 click_here  Icon User is offline

  • D.I.C Head

Reputation: 28
  • View blog
  • Posts: 178
  • Joined: 25-November 13

What is the best way to expand this program?

Posted 28 November 2013 - 02:07 AM

Hey all,

I have created a class which inherits from the PictureBox - Its purpose is to display a screenshot of a map (bounding box from OpenStreetMaps) and draw a point on it using Graphics and Pen. This is done by using the "paint" event and some class variables which are calculated points from Latitude and Longitude inputs.


        private void LocationMap_Paint(object sender, PaintEventArgs e)
        {
            e.Graphics.DrawRectangle(
            new Pen(Color.Red, 2f),
            xAxis, yAxis, 3, 3);
        
        }



It does this for one point, but I'd like to be able to do this for any number of points for the user.

My question is what is the best way to approach the problem?

My idea was to have an array of classes, where each class is a point with its own pen and graphics object. The drawing of each point could be done using the "foreach" loop, and I could try and add a new element to the array whenever the user wants to enter a new point. This runs into limitations when the user wants to remove a point. In C I would have just used a linked-list, but I don't know how to approach this problem.

Because I'm new at OOP, I'd like to hear some opinions and ideas

Is This A Good Question/Topic? 0
  • +

Replies To: What is the best way to expand this program?

#2 mavarazo  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 36
  • View blog
  • Posts: 182
  • Joined: 25-October 10

Re: What is the best way to expand this program?

Posted 28 November 2013 - 02:38 AM

why u dont use LinkedList in c#?
Was This Post Helpful? 0
  • +
  • -

#3 click_here  Icon User is offline

  • D.I.C Head

Reputation: 28
  • View blog
  • Posts: 178
  • Joined: 25-November 13

Re: What is the best way to expand this program?

Posted 28 November 2013 - 02:50 AM

View Postmavarazo, on 28 November 2013 - 02:38 AM, said:

why u dont use LinkedList in c#?


I was under the impression that pointers should be avoided in C# - However, I can see how this can be done with a little help from google.

Is this the best way of approaching the problem? Are there any other ways of doing this?
Was This Post Helpful? 0
  • +
  • -

#4 Michael26  Icon User is offline

  • DIC-head, major DIC-head
  • member icon

Reputation: 355
  • View blog
  • Posts: 1,521
  • Joined: 08-April 09

Re: What is the best way to expand this program?

Posted 28 November 2013 - 03:10 AM

You have LinkedList<T> Class, you could start there.
Was This Post Helpful? 1
  • +
  • -

#5 click_here  Icon User is offline

  • D.I.C Head

Reputation: 28
  • View blog
  • Posts: 178
  • Joined: 25-November 13

Re: What is the best way to expand this program?

Posted 28 November 2013 - 03:24 AM

View PostMichael26, on 28 November 2013 - 03:10 AM, said:

You have LinkedList<T> Class, you could start there.


This looks like a great place to start - Thanks for that!

I think that this will set up a good foundation for a growing program.
Was This Post Helpful? 0
  • +
  • -

#6 Michael26  Icon User is offline

  • DIC-head, major DIC-head
  • member icon

Reputation: 355
  • View blog
  • Posts: 1,521
  • Joined: 08-April 09

Re: What is the best way to expand this program?

Posted 28 November 2013 - 03:25 AM

Yes, anything related to C# starts with MSDN.
Was This Post Helpful? 1
  • +
  • -

#7 jhouns  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 25
  • View blog
  • Posts: 100
  • Joined: 21-December 09

Re: What is the best way to expand this program?

Posted 28 November 2013 - 03:28 AM

You're definitely on the right track. The way you have suggested is actually one of the many ways it can be done, I can only offer a few 'improvements'.

When you are drawing, it isn't a good idea to instantiate objects inside of the drawing loop (wherever possible) as it can slow it down far too much, logic should set it all up and then drawing should just visualise it. If you want to use a standard colour for your Pen, I recommend using:
Pens.Red


Otherwise, presuming you have a button or something to set the colour, you should do so then prior to being drawn.

Now getting to your ACTUAL question. An array of some kind of 'Dot' object is 'spot' on (hehe). Although you don't require each object to have it's OWN graphics object, that would take up a large amount of memory and possibly cause issues if you wanted to change the drawing surface.

You want to think of it like this: The object only needs to know HOW to draw itself, it doesn't need to know what it's drawing itself on, that's handled by the Graphics class, so when it's drawn you should actively tell it which graphics instance to use i.e:
class MyClass
{

 public void Draw(Graphics g)
 {
    // drawing code here
    g.fillRect...
 }

}



That way it doesn't need to know whether the graphics object draws to a picture box, a form or anything else and you don't have to worry about it while writing the drawing code.

For storage, I would actually recommend a list as it's a dynamic structure:
List<YourClass> lstPoints = new List<YourClass>(); // New list
YourClass x = new YourClass(); // new object
lstPoints.add(x); // add to list
lstPoints.RemoveAt(0); // remove from list at point 0 
lstPoints.Clear(); // Empty all items, useful for if you wanted to clear the screen.



I may have just confused you as the explanation isn't the best, but I can clear up anything I've said that you're not sure on.
Was This Post Helpful? 0
  • +
  • -

#8 click_here  Icon User is offline

  • D.I.C Head

Reputation: 28
  • View blog
  • Posts: 178
  • Joined: 25-November 13

Re: What is the best way to expand this program?

Posted 28 November 2013 - 04:31 AM

Thanks for the reply, jhouns.

I didn't think of using a custom colour - That's a great idea. I am using red and changing the map to a black and white photo: The shock of the red is very easy to see.

I have used a LinkedList (mainly because I entered it into the code before I read your response).

    public partial class LocationMap : PictureBox
    {
        ...
    
        LinkedList<DrawingPoint> Points = new LinkedList<DrawingPoint>();

        ...

        public void AddLocation(decimal Latitude, decimal Longitude)
        {
            try
            {
                Points.AddLast(new DrawingPoint(Latitude, Longitude));
            }
            catch (Exception)
            {
                throw;  
            }

        }

        private void LocationMap_Paint(object sender, PaintEventArgs e)
        {
            Pen CirclePoint = new Pen(Color.Red, 2f);

            foreach (DrawingPoint d in Points)
            {
                e.Graphics.DrawRectangle(
                CirclePoint,
                d.xPoint, d.yPoint, 3, 3);
            }

            /*e.Graphics.DrawRectangle(
            new Pen(Color.Red, 2f),
            xAxis, yAxis, 3, 3);*/
        
        }



Why would I chose a list<> instead of a linkedlist<>?
Was This Post Helpful? 0
  • +
  • -

#9 Ryano121  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1362
  • Posts: 3,002
  • Joined: 30-January 11

Re: What is the best way to expand this program?

Posted 28 November 2013 - 06:05 AM

Depends entirely on what you want to do with it.

A List<T> is implemented behind the scenes as a simple array that will grow and shrink as needed.

A LinkedList<T> is implemented as a set of nodes. Each nodes holds a value and a pointer to the next and previous nodes in the list.

As I said it depends on what you want to do with them. LinkedLists tend to take up a little more space (for the pointers), but they are very quick at item removal and insertion in the middle of the list.

A List on the other hand is much simpler and could at simple adding and removal from the end. However Lists are not good if you want to remove the first element or insert a new item at the beginning.
Was This Post Helpful? 1
  • +
  • -

#10 jhouns  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 25
  • View blog
  • Posts: 100
  • Joined: 21-December 09

Re: What is the best way to expand this program?

Posted 28 November 2013 - 06:34 AM

Ryano is bang on with that explanation. With one exception, adding items to the start is very simple to do thanks to the 'Insert' function, but it does mean a large list requires entirely re-processing to update, perhaps that's what he meant? Easy to do but expensive (in terms of processing) to run. Using a LinkedList over a List here is perfectly fine and works well, I'm just more accustomed to using List at this point in time.

Your implementation as well is good, if I could change on thing though, I would move this line
Pen CirclePoint = new Pen(Color.Red, 2f)


to line 6 (in the above example).

The reason for this is that every time you paint the window (potentially hundreds of times per second depending on your use) you have to declare a space in memory and create a new Pen object to fill it. This takes time and if it isn't going to change then you're causing a large amount of extra work to be done.

Imagine you need to draw a picture on a piece of paper, but every time you put your pens away after making a change to it. Then when you need to add something (re-draw the UI) you need to get your pen back out (re-declare/instantiate the object).

If there's the potential to have different coloured points, you could even move that pen to a property inside of DrawingPoint. Although it's not needed it is something to think about if you get bored.

Anyway, even with all that said, that code you posted will serve you well and will work fine. Good job and even better is that you've clearly shown an interest to working it out and not just asking for a solution.

Good luck!

This post has been edited by jhouns: 28 November 2013 - 06:36 AM

Was This Post Helpful? 1
  • +
  • -

#11 Ryano121  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1362
  • Posts: 3,002
  • Joined: 30-January 11

Re: What is the best way to expand this program?

Posted 28 November 2013 - 08:19 AM

Yeah thats what I meant. Doing all the common operations on any List is very easy to do. They all expose the same interface to do so.

However it should be still taken into account what's going on behind the scenes when you use those methods. They may be easy to do, but in terms of efficiency inserting an item at the front of an array based list involves shifting all the other elements. Whereas in a LinkedList a node new can just be created and some pointers modified = O(1).

But really either will work fine. Unless you are scaling this to thousands of elements, the speed differences won't matter a huge amount. That being said its always good to use the right data structure from the get go.
Was This Post Helpful? 0
  • +
  • -

#12 jhouns  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 25
  • View blog
  • Posts: 100
  • Joined: 21-December 09

Re: What is the best way to expand this program?

Posted 28 November 2013 - 10:42 AM

Ryano, sounds like you and me are saying the same thing in different ways, but we're getting off topic here. You are of course completely correct.
Was This Post Helpful? 0
  • +
  • -

#13 click_here  Icon User is offline

  • D.I.C Head

Reputation: 28
  • View blog
  • Posts: 178
  • Joined: 25-November 13

Re: What is the best way to expand this program?

Posted 28 November 2013 - 03:11 PM

Quote

I would move this line... to line 6


Good point

Quote

If there's the potential to have different coloured points, you could even move that pen to a property inside of DrawingPoint. Although it's not needed it is something to think about if you get bored.


It's probably worth investigating. I will probably look into inserting an image for each point, so I will be looking at a Graphics object in DrawingPoint at some stage.

The user probably will set up a few points, and then leave it like that for the rest of the time the program is opened (following them on screen). Therefore, a fast insert is probably not a priority in this application: Rather, a fast access time would be more desired.

Based on a few google searches (and the conclusions I have gathered from it) I will chose a list instead of a linkedlist. The reason is that it appears that using a list has faster access time, but slower insert and delete time (vs. LinkedList).

Quote

Good job and even better is that you've clearly shown an interest to working it out and not just asking for a solution

:)/>/>
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1