Help with math

The Partridge Family were neither partridges nor a family. Discuss.
egizz983
Posts: 311
Joined: August 27th, 2016, 12:30 pm

Help with math

Post by egizz983 » September 7th, 2016, 4:48 pm

Hello guys dont want to ask :roll: but i need your help with a math calculations , i want to start draw a picture from the bottom right corner , so i need to get a pixels backward too and , but as many times i have tried i could find right calculations to find a current index , to draw farward function was

Code: Select all

for (int a = 0; a < heigth; a++) {
	for (int b = 0; b < width; b++) {
		PutPixel(x +b ,y + a , image[b + a * width].GetR(), image[b + a * width].GetG(), image[b + a * width].GetB());
	}
}
and now i need a calculations to get pixels backward from 4000 to 0, cuz example width 50 height 80 , i am bad at math and could not figure this out :D

freebattie
Posts: 197
Joined: August 4th, 2016, 10:05 am

Re: Help with math

Post by freebattie » September 7th, 2016, 5:33 pm

you just want to change the order the picture is drawn ?or flip it? if u just want to start drawing same picture for bottom right corner you do (a= height - 1; a >= 0;a++) and same whit b i think
and if you want to flip the colors you do this img[(a +((height - 1) ) - b) * width] that how i flip my img at least. but maybe i misunderstand what you ask

egizz983
Posts: 311
Joined: August 27th, 2016, 12:30 pm

Re: Help with math

Post by egizz983 » September 7th, 2016, 5:46 pm

freebattie wrote:you just want to change the order the picture is drawn ?or flip it? if u just want to start drawing same picture for bottom right corner you do (a= height - 1; a >= 0;a++) and same whit b i think
and if you want to flip the colors you do this img[(a +((height - 1) ) - b) * width] that how i flip my img at least. but maybe i misunderstand what you ask
color formula doent mach width 50 height 80

Code: Select all

for (int a = heigth; a > 0; a--) {
		for (int b = width; b > 0; b--) {

 (a +((height - 1) ) - b) * width // (80 + (80 - 1 ) - 50) *50 = 116k instead of 4k 
}
}
what i am asking is , usually at least me start drawing a picture from top left corner to the right bottom corner right , so i need a formula for a backwards from bottom right corner to the top left corner , so formula should return result from width*height to 0 , as example width 50 heigth 80 so from 4000-0 ..

Edited:
okay i got this formula : (b-1) + a * width , its works fine :)

Code: Select all

for (int a = heigth-1; a > 0; a--) {
		for (int b = width; b > 0; b--) {

 (b-1) + a * width
}
}

freebattie
Posts: 197
Joined: August 4th, 2016, 10:05 am

Re: Help with math

Post by freebattie » September 7th, 2016, 7:04 pm

well for you to put the first color you read at buttom right you need to do
(a = height - 1 ; a >= 0;a--) and same for b(b = width - 1 ; b >= 0;b--), then you will start your put pixel at button right and draw it upwords.

if you whant to get the colors backwords you do;

[x +((height -1) - y) * width]
50 +((80- 1 ) - 80 ) 50 =
79 - 80 = -1
50 * -1 = -50
50 - 50 = 0
and if a is 0 and b is 1 you get =
1 + (80 - 1) -0) *50 = 4000

freebattie
Posts: 197
Joined: August 4th, 2016, 10:05 am

Re: Help with math

Post by freebattie » September 7th, 2016, 7:44 pm

whit your fomula you will go over the array if you at 80 and 50,
50- 1 = 49
80*50 = 4000
49 + 4000 = 4049
c++ does everything inside () first then it does * and / then it does - and +
as i understand it, maybe i am wrong

egizz983
Posts: 311
Joined: August 27th, 2016, 12:30 pm

Re: Help with math

Post by egizz983 » September 7th, 2016, 7:53 pm

this is how i solved this :)

Code: Select all

void Graphics::DrawSpriteBackW(int x, int y, int width, int heigth, std::vector<Color>& image)
{
	for (int a = heigth-1; a > 0; a--) {
		for (int b = width; b >= 0; b--) {
		PutPixel((x+width) - (width -b), (y+heigth) - (heigth- a), image[b - 1 + a*width].GetR(), image[b - 1 + a*width].GetG(), image[b - 1 + a*width].GetB());

		}
	}
}
but its still are no perfect i mean what do i do with this is a clipping effect (you fire bullet witch goes y--) once it reach y == 0 you start drawing a backward and --height, so this will creates clipping effect but at the end you can see some drawing bug where the pixel are no mach or some even not draw(black)
Image
i have finished clipping effect only with a machine gun type weapon , witch is inside DrawBullets function
Attachments
Chili Framework 2016 - Copy (2).rar
(328.76 KiB) Downloaded 134 times

albinopapa
Posts: 4373
Joined: February 28th, 2013, 3:23 am
Location: Oklahoma, United States

Re: Help with math

Post by albinopapa » September 7th, 2016, 9:47 pm

To draw a sprite backward, or flipped from right to left:

Code: Select all

     // width 50, height 80
     // Your x values for drawing: 0, 1, 2, 3, 4, 5, ...
     // Your x values for surface: 49, 48, 47, 46, 45, 44, ...
     // Your loops will be going forward, from 0 to 49, and your image index will be going from 49 to 0
	for( int iy = 0; iy < height; ++iy )
	{
		for( int ix = 0; ix < width; ++ix )
		{
			int surfaceIndex = ( ( width - 1 ) - ix ) + ( iy * width );
			PutPixel( x + ix, y + iy, image[ surfaceIndex ] );
		}
	}
To flip top to bottom, you'd do almost the same thing.
int sufaceIndex = (ix + ((height - iy - 1) * width));

To flip both right to left and top to bottom.
int surfaceIndex = (width -1 - ix) + ((height - iy - 1) * width);

This formula doesn't take into account any clipping though. It seems you are decreasing the height to be your clipping guide, there might be a better way to handle clipping though.
If you think paging some data from disk into RAM is slow, try paging it into a simian cerebrum over a pair of optical nerves. - gameprogrammingpatterns.com

NaturalDemon
Posts: 97
Joined: October 28th, 2012, 8:28 pm

Re: Help with math

Post by NaturalDemon » September 7th, 2016, 11:18 pm

Hmmmm, if i remember correctly, you draw several hundreds of frames per second depending on your hardware.
But you only need max 60 Fps, i believe you see pixels from the previous frame drawn over the current frame.

You should build a state machine with a timer .... That updates a complete drawn frame instead of a partial frame to the gpu buffer once per 16 milisecond (ms). 1000 ms / 60 fps = 16.xxx ms

You only have 1 "backbuffer" and you keep updating those pixels while presenting to the gpu.
You could make a second buffer and alternate between them.
But you need a mechanism to slow down.

User avatar
LuisR14
Posts: 1248
Joined: May 23rd, 2013, 3:52 pm
Location: USA
Contact:

Re: Help with math

Post by LuisR14 » September 7th, 2016, 11:45 pm

probly wrong thread NaturalDemon :D

edit:
anyways about the flip image, you just have to flip either the output coord or the image access(if you do both then in effect you'd be drawing as if normal)
(following would have a screen offset for out coords tho, which would be flipped too hehe xD)

Code: Select all

normal x, and img.x:
for( int y = 0; y < height; y++ )
{
	for( int x = 0; x < width; x++ )
	{
		DrawPixel( x, y, image[ x + y * width ] ); // top/left-to-bottom/right image access -> screen top/left-to-bottom/right
	}
}

reverse x, normal img.x: (draw img to flipped screen)
for( int y = 0; y < height; y++ )
{
	for( int x = 0; x < width; x++ )
	{
		DrawPixel( width - x, y, image[ x + y * width ] ); // top/left-to-bottom/right image access -> screen top/right-to-bottom/left
	}
}

normal x, reverse img.x: (flip image)
for( int y = 0; y < height; y++ )
{
	for( int x = 0; x < width; x++ )
	{
		DrawPixel( x, y, image[ width - x + y * width ] ); // top/left-to-bottom/right image access -> screen top/left-to-bottom/right
	}
}

reverse x, and img.x: (draw flipped img to flipped screen, hence same as normal)
for( int y = 0; y < height; y++ ) // top-to-bottom loop
{
	for( int x = 0; x < width; x++ ) // left-to-right loop
	{
		DrawPixel( width - x, y, image[ width - x + y * width ] ); // top/right-to-bottom/left image access -> screen top/right-to-bottom/left
	}
}
to flip the offset would mean

Code: Select all

normal x: offset = x; as normal haha
reverse x: offset = (scrnwidth - img.width - x) - (scrnwidth - (scrnwidth - (x + img.width) * 2)) - img.width
(dam that's some formula, easier to go the non-flipped screen mode xD, maybe someone can simplify it haha)
always available, always on, about ~10 years c/c++, java[script], win32/directx api, [x]html/css/php/some asp/sql experience. (all self taught)
Knows English, Spanish and Japanese.
[url=irc://irc.freenode.net/#pchili]irc://irc.freenode.net/#pchili[/url] [url=irc://luisr14.no-ip.org/#pchili]alt[/url] -- join up if ever want real-time help or to just chat :mrgreen: --

albinopapa
Posts: 4373
Joined: February 28th, 2013, 3:23 am
Location: Oklahoma, United States

Re: Help with math

Post by albinopapa » September 8th, 2016, 1:32 am

NaturalDemon wrote:Hmmmm, if i remember correctly, you draw several hundreds of frames per second depending on your hardware.
But you only need max 60 Fps, i believe you see pixels from the previous frame drawn over the current frame.

You should build a state machine with a timer .... That updates a complete drawn frame instead of a partial frame to the gpu buffer once per 16 milisecond (ms). 1000 ms / 60 fps = 16.xxx ms

You only have 1 "backbuffer" and you keep updating those pixels while presenting to the gpu.
You could make a second buffer and alternate between them.
But you need a mechanism to slow down.
Yeah, if you are on the correct thread, the chili framework is already double-buffered, so the frames don't get updated until all is drawn to back buffer, so it's not an issue of seeing pixels from previous frames.
If you think paging some data from disk into RAM is slow, try paging it into a simian cerebrum over a pair of optical nerves. - gameprogrammingpatterns.com

Post Reply