Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

Name-ing-Way

Creating a perfect square...

This topic is 5853 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I''m sorry if I am double posting this, I thought I posted this about 20 minutes ago but I cannot find it anywhere, so I am going to try again. I am writing a chess program for two players to play a game of chess on the computer. I am doing the graphics for this in dos with bgi graphics. I am using the libraries graphics.h and dos.h to perform most of the graphics functions in this program. The program is being written in C++. Ok, now for my problem. I am trying to create a perfect square on one side of the screen for my chess board. To do this, I am using the function: rectangle(topleftx, toplefty, bottomrightx, bottomrighty) where as the four variables are the coordinates for the top left point of the rectangle and the bottom right point of the rectangle. For some reason no matter how right I think my formulas are, it will not create a PERFECT square. The width is longer than the height of the board. I know this because I measured it with a ruler on my monitor ( hehe ). It becomes even more apparant when i put the indiviual squares into the chess board, because the h-file squares (for those non-chess enthusiasts that is the right hand column of squares) are rectangles and not perfect squares like the rest. The problem is not that pixels are longer one way than the other because I am fixing this by using the aspect ratio of the pixels. Here is coding of drawing the outer border of my chess board: int Xaspect, Yaspect, Left, Right, Top, Bottom; double Ratio; getaspectratio(&Xaspect, &Yaspect); setbkcolor(BLACK); setcolor(WHITE); Left = getmaxx() * 0.1; Top = getmaxy() * 0.2; Bottom = getmaxy() * 0.8; Ratio = double(Yaspect) / double(Xaspect); Right = (getmaxx() * 0.1) + ((getmaxy() * 0.6) * Ratio); rectangle(Left, Top, Right, Bottom); If anyone could help me know as to why this isn''t creating a perfect square it would be a huge help. Thanx in advance.

Share this post


Link to post
Share on other sites
Advertisement
God, I remember BGI. Ahhh, I had a nice dart game in that. My favorite was my Paper-Scissors-Rock though. :-)

My advice, don''t use BGI, but that won''t help you any. I don''t know if that function is built into the API but I reccomend just plotting the pixels yourself (if you don''t already). Also, check out the info about which screen mode you are in. In DX you have to worry about screen pixel pitch when modifying a texture. That might have something to do with it. I really reccomend you try OpenGL if that is a posibility. I''m not sure what you''re doing with your code right there but it probably has to do with the aspect ratio. Tune that or just do this:


  
top = 0;
bottom = 100;
for ( int i = 0; i < iLineWidth; i++ )
{
// Put a pixel at i column at row 0 (draw a line).

putpixel( i, 0, RED );
}


Than just do this for the bottom and left and right. To fill it would not be too tough. Inefficient, but when nothing else works...

Share this post


Link to post
Share on other sites
Consider the controls you have on the monitor, they can adjust the width and height of what is displayed without your program knowing anything about it. Following this through, you end up with the conclusion that you can either a) accept non square squares or b) allow the user to set the aspect ratio. This would just be a screen where they can make a box wider until it becomes square. Once this is done, store the ratio of height:width and use that to scale everything.

Trying is the first step towards failure.

Share this post


Link to post
Share on other sites
Oh yeah, I forgot - sub-pixel accuracy. If it really matters, you are going to have to use sub-pixels. Basically, a line 1 and a half units long would look like this:

Black Pixel, White Pixel, Grey Pixel, Black Pixel
or

Black Pixel Dark Grey Pixel, White Pixel, Dark Grey Pixel, Black Pixel

and this may become noticable too.



Trying is the first step towards failure.

Share this post


Link to post
Share on other sites
Dude, what screen mode? Some screen modes have non-square pixels. As wierd as it sounds, that''s probably your problem.

Share this post


Link to post
Share on other sites
a 20x20 pixel box is square regardless of the video mode.

sometimes the settings for your moniter width/height is off. you dont fiddle with the aspect ratio when drawing pixels, that will just screw your square up.

non square pixels dont change a thing. if its on the screen at wxh resolution and the screen is being displayed properly, 20x20 is ALWAYS square.

just understand, its not your job to "fix" the aspect ratio within your game.

Share this post


Link to post
Share on other sites
I get what you are saying, but if it was just the monitor, then all the boxes within the chessboard would be streched too. This isn't the case. All of the boxes within the board are perfect squares except for the bottom row and the righthand column.

Kinda like this:

_________________
|_|_|_|_|_|_|_|__|
|_|_|_|_|_|_|_|__|
|_|_|_|_|_|_|_|__|
|_|_|_|_|_|_|_|__|
|_|_|_|_|_|_|_|__|
|_|_|_|_|_|_|_|__|
|_|_|_|_|_|_|_|__|
|_|_|_|_|_|_|_|__|



If it were just the monitor, all the squares would be streched equally. I *think* it might have something to do with integers disregarding the decimals so all the decimal that is left over was placed in the outer squares...but I still don't know what to do.


[edited by - Name-ing-Way on June 9, 2002 5:55:51 PM]

Share this post


Link to post
Share on other sites
It's not gauranteed that a a 20x20 pixel plotted area will be square. You need to determine the aspect ratio of the graphics mode, and use it as a scaling factor when drawing. IIRC, it's just a magic number you knew a-priori; back-in-the haydays it varied depending on the graphics mode and graphics card (they were not all the same).

SVGA 1024x768 & 640x480 have square pixels. VGA 320x240 had an aspect ratio of .6 (I think) - so you would have to plot 20x12 to get a square.

Anyway, Name-ing-way appears to correct for it already at that.

It could very-well be alaising error (integer round-off in this case). Drawing the lines with sub-pixel accuracy might correct the problem. Or don't pick the edges of the board by arbitrary scalers - pick a width & height that's pixel perfect!

[edited by - Magmai Kai Holmlor on June 9, 2002 7:05:18 PM]

Share this post


Link to post
Share on other sites
quote:
Original post by Name-ing-Way
I know this because I measured it with a ruler on my monitor ( hehe ).


That''s -SO- cute! I could just eat you up!..



Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!