#### Archived

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

# Creating a perfect square...

## Recommended Posts

Name-ing-Way    122
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 on other sites
Dirge    300
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 on other sites
ragonastick    134
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 on other sites
ragonastick    134
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 on other sites
Puzzler183    540
Dude, what screen mode? Some screen modes have non-square pixels. As wierd as it sounds, that''s probably your problem.

##### Share on other sites
Name-ing-Way    122
The graphics mode is VGAMED (640x350, 16 colors)

##### Share on other sites
a person    118
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 on other sites
Name-ing-Way    122
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 on other sites
Shannon Barber    1681
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 on other sites
munkie    122
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 on other sites
bishop_pass    109
quote:
Original post by Name-ing-Way
The graphics mode is VGAMED (640x350, 16 colors)

That's your problenm right there. 640x480 resolutions have square pixels. It stands to reason that 640x350 resolutions then could not have square pixels.

350 / 480 = .7291666
Your square should be rendered as a box where its width is equal to its width and its height is equal to its width * .7291666.

[edited by - bishop_pass on June 10, 2002 2:07:41 PM]

##### Share on other sites
jjmontes    158
Magmai, 320x240 is square too (it''s linear with 640x480).

Anyway, if the problem is that pixels on the sides of the screen are longer, it is due to the Pincushion and Trapezoid controls of the monitor. These can strech the image at the sides.

I think there can not be any other posibility. The aspect ratio could be also a problem but it would never make some pixels longer than others.

In my opinion, bother only about the aspect ratio, and let the people touch their monitor controls if they think they have to.

##### Share on other sites
Stoffel    250
Most screen resolutions are 4:3, but most monitors are 4:3 as well, so...shouldn''t there be no problem here?

##### Share on other sites
Name-ing-Way    122
The pixels not being square has nothing to do with it, because I already compensated for that. (Using the aspect ratio).

##### Share on other sites
Guest Anonymous Poster
how big is your board? Is each square like 10x10 or 1x1? If it is a small number rounding errors would make a difference. Also while you are compensating for aspect ratio maybe that is causing a rounding error. If you haven''t already try it on a normal 4 to 3 ration. I mean you are compensating for it, but sometimes there is some other error elsewhere and everything messes up. Sometimes after exausting all logical solutions you try random things, and then you find that your error was somewhere else entirely, though hidden by a bunch of things that hide it most of the time.

##### Share on other sites
a person    118
i guess i should explain better.

all moniters 4:3 ratio (like tvs). ALL standard resolutions:

320x240
400x300
512x384
640x480
800x600
1024x768
1152x864
1280x960
1600x1200

are also 4:3 ratio, and can be adjusted to full the moniter screen using the moniter controls.

other resolutions such as 320x200 require the moniter controls to be adjusted so it displays properly (ie with the black borders). if this is not done, the image will be distorted. the alternative would be do do the aspect ration fix with the software. this is ill advised since ppl with their moniters set correctly will end up with a distorted image.

this is why some suggest only 4:3 resolutions. those that allow the other resolutions as well, odnt compensate for the different aspect ratio because it is (rightfully) assumed that the user (or manufacturer) set the moniter to show the correct aspect ratio so things appear as they should.

again, NO fiddling is required for an 4:3 resolution (the ones i listed above). doing so, will screw things over. if you do decide to do fiddling, remeber 4:3 is the target aspect ratio, not 1:1.

if the moniter and card are working (and setup) properly, any non 4:3 aspect ratio will be letterboxed, just like when movies are released in the 4:3 format and then letterboxed since the original was 16:9.