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

Author Topic: [SOLVED] Problem with storing generated pixel locations  (Read 2650 times)

0 Members and 1 Guest are viewing this topic.

TheGoldenFyre

  • Newbie
  • *
  • Posts: 18
    • View Profile
[SOLVED] Problem with storing generated pixel locations
« on: April 17, 2016, 07:12:31 am »
So, I'm trying to create semi-random pixels, with the middle of the window as a starting point. My only problem is, that I need to store the x and y-axis of the new pixel that I generate.

So say, I have a single pixel (5, 5). Then first I want to generate a layer around that pixel (4,5), (5,4) (5,6) and (6,5) (I know how to do this). But after this, I want to generate a simular layer around the new pixels, and I would need to store there locations, and the locations of all pixels to come. Is there an easy way to to this? Because I wouldn't know of any on this scale. (I'm creating a image of 700 by 700 pixels)

Thanks for any help!
« Last Edit: April 17, 2016, 08:56:59 pm by TheGoldenFyre »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10801
    • View Profile
    • development blog
    • Email
AW: Problem with storing generated pixel locations
« Reply #1 on: April 17, 2016, 01:52:39 pm »
You can store pixels for example by using an sf::Image.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6286
  • Thor Developer
    • View Profile
    • Bromeon
Re: Problem with storing generated pixel locations
« Reply #2 on: April 17, 2016, 02:09:34 pm »
The representation depends on how much information you need to store. As eXpl0it3r says, sf::Image is a straightforward way to store pixels. Also because it allows direct conversion into sf::Texture for rendering.

If you only have a few bits (<32) of information per pixel, e.g. whether a pixel is set or if it has a predefined color, use std::vector with the smallest type that can represent your pixel. If the total number of pixels is tiny compared to the area, you could think about a sparse representation (std::map<sf::Vector2f, Pixel, VectorComparator>), but I don't think it's needed right now.
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

TheGoldenFyre

  • Newbie
  • *
  • Posts: 18
    • View Profile
Re: Problem with storing generated pixel locations
« Reply #3 on: April 17, 2016, 02:34:14 pm »
I understand how to store the pixels color, but I'm wondering what would be a good way to store the location of the (newly generated) pixels. I'm already storing pixels in a sf::Image to create a texture by the way

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10801
    • View Profile
    • development blog
    • Email
AW: Problem with storing generated pixel locations
« Reply #4 on: April 17, 2016, 03:28:18 pm »
With an sf::Image you have the function setPixel(x, y, color).
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

shadowmouse

  • Sr. Member
  • ****
  • Posts: 302
    • View Profile
Re: Problem with storing generated pixel locations
« Reply #5 on: April 17, 2016, 03:37:32 pm »
I'm fairly sure the problem is knowing which pixels have been modified, not modifying the pixels themselves, in which case, I think you are going about it wrong. If you want to make pixels expanding out from the centre, just make a diamond shape of different sizes around the central pixel. e.g. first you start with a pixel x,y then you make a loop that does (x+1,y),(x-1,y),(x,y+1),(x,y-1), then make a loop that does (x+2,y),(x+1,y+1),(x,y+2),(x-1,y+1),(x-2,y),(x-1,y-1),(x,y-2),(x+1,y-1), etc.

EDIT: unless of course you do actually want to store all modified pixels and have the function go on forever as each one modifies all the ones around it, which will never stop.

TheGoldenFyre

  • Newbie
  • *
  • Posts: 18
    • View Profile
Re: Problem with storing generated pixel locations
« Reply #6 on: April 17, 2016, 05:11:12 pm »
Quote
I'm fairly sure the problem is knowing which pixels have been modified, not modifying the pixels themselves, in which case, I think you are going about it wrong. If you want to make pixels expanding out from the centre, just make a diamond shape of different sizes around the central pixel. e.g. first you start with a pixel x,y then you make a loop that does (x+1,y),(x-1,y),(x,y+1),(x,y-1), then make a loop that does (x+2,y),(x+1,y+1),(x,y+2),(x-1,y+1),(x-2,y),(x-1,y-1),(x,y-2),(x+1,y-1), etc.

EDIT: unless of course you do actually want to store all modified pixels and have the function go on forever as each one modifies all the ones around it, which will never stop.

Since I'm trying to create a image of 700 by 700, is there a way to do this automaticly? Otherwise my program would become huge.

EDIT: By the way, thank you. This is what I'm trying to do, but I will need to make something that can write the code you suggested for me
« Last Edit: April 17, 2016, 05:30:07 pm by TheGoldenFyre »

shadowmouse

  • Sr. Member
  • ****
  • Posts: 302
    • View Profile
Re: Problem with storing generated pixel locations
« Reply #7 on: April 17, 2016, 05:46:33 pm »
void createDiamondShape(int x, int y, int size)
{
    for(int i(0); i <= size; ++i) {
        if(i!=size) {
            SetPixel(x-i,y+(size-i));
            SetPixel(x+i,y-(size-i));
        }
        if(i!=0) {
            SetPixel(x+i,y+(size-i));
            SetPixel(x-i,y-(size-i));
        }  
    }
}
 
This seems to make the right shape for anything other than a size of 0. The x and y you pass are the centre of the diamond and the size is how many horizontal or vertical moves it would take to get from the centre to the edge of the diamond. Just call it with an incrementing size value and implement your own random colour algorithm.

TheGoldenFyre

  • Newbie
  • *
  • Posts: 18
    • View Profile
Re: Problem with storing generated pixel locations
« Reply #8 on: April 17, 2016, 08:56:40 pm »
Thank you very much ShadowMouse! I'm trying get into algorithm making, but this I just couldn't figure out.  :)