Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Array Points  (Read 7445 times)

0 Members and 1 Guest are viewing this topic.

Wander

  • Full Member
  • ***
  • Posts: 170
    • View Profile
    • Email
Array Points
« on: September 11, 2010, 07:21:18 pm »
I'm working on a map program. I have it working where it displays coordinate on the map, but I can't figure out how to get it to display multiple coordinates without creating a new sf::Shape for each coordinate.
-Wander

Kingdom of Fish

  • Jr. Member
  • **
  • Posts: 72
    • View Profile
Array Points
« Reply #1 on: September 12, 2010, 03:09:01 am »
Just draw the same shape several times each frame...

Code: [Select]
Draw(shape)
shape.SetPosition()
Draw(shape)
shape.SetPosition()
Draw(shape)
Display()

Wander

  • Full Member
  • ***
  • Posts: 170
    • View Profile
    • Email
Array Points
« Reply #2 on: September 12, 2010, 03:20:52 am »
That would require me to manually account for how many points I have.

I forgot to mention what I actually want to do. Let me clear it up:

I want the user to enter a coordinate. The map will mark that coordinate and go about it's business. Then say 5 minutes later, the user wants to mark another coordinate. They enter it and the program displays both coordinates. Then say a few minutes later they have another coordinate to enter. It would have to display all three.
Since I want the user to input over 200 coordinates, your method would be impractical and I don't know which coordinates they would want to access.

:)
-Wander

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Array Points
« Reply #3 on: September 12, 2010, 10:05:22 am »
Why don't you want to have one sf::Shape for each coordinate?
Laurent Gomila - SFML developer

Wander

  • Full Member
  • ***
  • Posts: 170
    • View Profile
    • Email
Array Points
« Reply #4 on: September 12, 2010, 10:08:50 am »
Then I would have to manually have to program in 200 sf::Shapes.... Wouldn't I?
-Wander

panithadrum

  • Sr. Member
  • ****
  • Posts: 304
    • View Profile
    • Skyrpex@Github
    • Email
Array Points
« Reply #5 on: September 12, 2010, 12:13:57 pm »
I think you need some minimal programming skills before going any further!

Wander

  • Full Member
  • ***
  • Posts: 170
    • View Profile
    • Email
Array Points
« Reply #6 on: September 12, 2010, 12:20:38 pm »
I do.... I want to use for loops, but I can't think of any way to manage making the elements, display them, and print them in a text file.
-Wander

Kingdom of Fish

  • Jr. Member
  • **
  • Posts: 72
    • View Profile
Array Points
« Reply #7 on: September 12, 2010, 12:30:34 pm »
@Wander: I'm guessing you are using C++, look up std::vector. Otherwise look for lists in whatever language you use.

Disch

  • Full Member
  • ***
  • Posts: 220
    • View Profile
Array Points
« Reply #8 on: September 12, 2010, 05:47:44 pm »
I was going to recommend std::list instead of std::vector, but yeah.

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6286
  • Thor Developer
    • View Profile
    • Bromeon
Array Points
« Reply #9 on: September 12, 2010, 10:13:48 pm »
Quote from: "Disch"
I was going to recommend std::list instead of std::vector
Why?
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

Kingdom of Fish

  • Jr. Member
  • **
  • Posts: 72
    • View Profile
Array Points
« Reply #10 on: September 12, 2010, 11:35:12 pm »
Quote from: "Disch"
I was going to recommend std::list instead of std::vector, but yeah.


I would still recommend std::vector since this task does not seem to be in need of inserting stuff in the midle of the array (which is what std::list is good at).

Disch

  • Full Member
  • ***
  • Posts: 220
    • View Profile
Array Points
« Reply #11 on: September 13, 2010, 01:51:22 am »
I'd recommend list because:

- the input is user driven.  The user is adding points at will.  Where there's user addition there's often user removal.  Removing elements from a vector is painful.  Removing them from a list is harmless.

- the input can grow to infinity (again partially due to it being user driven).  And sparatic growths in a vector trigger reallocation.  Although granted this can be minimized with clever calls to reserve().

- random access doesn't seem to be necessary.  And since random access is pretty much the only advantageous reason to use vector over, say, list or deque, there's not much point to using vector.



My question would be why did you recommend vector?

I get the impression vector is just the default "go to" container for a lot of people when it probably shouldn't be.

Silvah

  • Guest
Array Points
« Reply #12 on: September 13, 2010, 10:15:45 am »
Quote from: "Disch"
the input can grow to infinity (again partially due to it being user driven).  And sparatic growths in a vector trigger reallocation.  Although granted this can be minimized with clever calls to reserve().
In a list, every single insertion triggers allocation. vector does not need to allocate memory each time the user has requested to insert an element (in fact, reallocations are not as frequent as one could think). vector wins.

Quote from: "Disch"
And since random access is pretty much the only advantageous reason to use vector over, say, list or deque, there's not much point to using vector.
It's not the only vector's advantage over other containers.


Quote from: "Disch"
My question would be why did you recommend vector?
Perhaps because of one thing constantly overlooked by people who "care" about performance, but never measure it - vectors (arrays in general) have the best possible cache locality. Lists (unless unrolled) don't have any. Sometimes this can have tremendous impact on performance, so it could be - and sometimes actually is - faster to use vector rather than list, even though the computational complexity will be worse.

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6286
  • Thor Developer
    • View Profile
    • Bromeon
Array Points
« Reply #13 on: September 13, 2010, 03:15:47 pm »
Quote from: "Disch"
Removing elements from a vector is painful.
It is not. This is only known by few people, but ignoring the element order, you can remove elements of a std::vector in O(1). And this is much faster than at std::list, because no dynamic deallocation comes to play.

Quote from: "Disch"
I get the impression vector is just the default "go to" container for a lot of people when it probably shouldn't be.
In the most cases, std::vector is the best choice. You should generally stick to it, and if you've got special requirements or measured performance problems, look for another container.

I think Silvah pointed the other advantages out quite well.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

Disch

  • Full Member
  • ***
  • Posts: 220
    • View Profile
Array Points
« Reply #14 on: September 13, 2010, 04:39:45 pm »
interesting indeed!

Thanks for the response.

 

anything