P
P
Popou2021-07-22 11:07:18
Mathematics
Popou, 2021-07-22 11:07:18

What's wrong with sine?

I wanted to make a similar wave effect :

60f923262805e176312543.jpeg

For this I used the following algorithm:

for (int y = 0; y < img.Height; ++y)
{
    Span<Rgba32> pixelRowSpan = img.GetPixelRowSpan(y);
    for (int x = 0; x < img.Width; ++x)
    {

        int y1 = Convert.ToInt32(y + 20.0 * Math.Sin(x / 32.0));

        if (y1 >= img.Height)
            y1 = img.Height - 1;
        if (y1 < 0)
            y1 = 0;

        var sourcePixel = img.GetPixelRowSpan(y1)[x];

        pixelRowSpan[x] = sourcePixel;
    }
}


But instead of the desired result, I get this:
irctn-afdpmwoufhtzaj0qufxme.jpeg

Original Image :
67427.jpg

Thanks Wataru!
Original Image :
s-9wp4wxny83thhluknnmirhcxe.jpeg

Result :
5tqvub57okwctvybjbf3ode3zq4.jpeg

Answer the question

In order to leave comments, you need to log in

1 answer(s)
W
Wataru, 2021-07-22
@Popou

You change the image in place by walking over it from top to bottom. When your sine shifts the pixels down, you overwrite all the pixels in the topmost column.
It is necessary either to copy with changes to a new image, or to change the direction of the passage depending on the sign of the sine.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question