The MASM Forum

General => The Campus => Topic started by: zedd151 on March 06, 2025, 04:52:36 AM

Title: Polygon Example
Post by: zedd151 on March 06, 2025, 04:52:36 AM
:biggrin:
Today I found out that it was extremely easy to create a filled polygon. I did not know that until I tried it today.
I used umpteen lines to create the X for tic-tac-toe.  :toothy:

(https://i.postimg.cc/ryHqJygN/Red-X.png)
    include \masm32\include\masm32rt.inc

    DlgProc proto :dword, :dword, :dword, :dword
       
    .data
        dlgname      db "Dialog Template", 0
        hInstance    dd 0

    xpoints label dword ; array of point structs
        POINT <28, 14>
        POINT <29, 14>
        POINT <69, 54>
        POINT <70, 54>
        POINT <110, 14>
        POINT <111, 14>
        POINT <125, 28>
        POINT <125, 29>
        POINT <85, 69>
        POINT <85, 70>
        POINT <125, 110>
        POINT <125, 111>
        POINT <111, 125>
        POINT <110, 125>
        POINT <70, 85>
        POINT <69, 85>
        POINT <29, 125>
        POINT <28, 125>
        POINT <14, 111>
        POINT <14, 110>
        POINT <54, 70>
        POINT <54, 69>
        POINT <14, 29>
        POINT <14, 28>
       
    .code

start:
   
        invoke GetModuleHandle, NULL
        mov hInstance, eax
        invoke DialogBoxParam, hInstance, 100, 0, addr DlgProc, 0
        invoke ExitProcess, eax
   
    DlgProc proc hWin:dword, uMsg:dword, wParam:dword, lParam:dword
    local hDC:dword, ps:PAINTSTRUCT, hBrush_red:dword, hBrush_red_old:dword
        .if uMsg == WM_INITDIALOG
          invoke SendMessage, hWin, WM_SETTEXT, 0, addr dlgname
        .elseif uMsg == WM_PAINT
          invoke BeginPaint, hWin, addr ps
          mov hDC, eax
         
          invoke CreateSolidBrush, 000000FFh          ; create red brush
          mov hBrush_red, eax
         
          invoke SelectObject, hDC, hBrush_red        ; select brush into hDC
          mov hBrush_red_old, eax
         
          invoke Polygon, hDC, addr xpoints, 24      ; draw polygon
         
          invoke SelectObject, hDC, hBrush_red_old
         
          invoke DeleteObject, hBrush_red            ; delete red brush
         
          invoke EndPaint, hWin, addr ps
        .elseif uMsg == WM_CLOSE
          invoke EndDialog, hWin, 0
        .endif
        xor eax, eax
        ret
    DlgProc endp

end start
Attached: polygon test.zip

Title: Re: Polygon Example
Post by: _japheth on March 06, 2025, 09:41:58 AM
Quote from: zedd151 on March 06, 2025, 04:52:36 AM    xpoints label dword ; array of point structs
        POINT <28, 14>
        POINT <29, 14>
        POINT <69, 54>
        POINT <70, 54>
        POINT <110, 14>
        POINT <111, 14>
        POINT <125, 28>
        POINT <125, 29>
        POINT <85, 69>
        POINT <85, 70>
        POINT <125, 110>
        POINT <125, 111>
        POINT <111, 125>
        POINT <110, 125>
        POINT <70, 85>
        POINT <69, 85>
        POINT <29, 125>
        POINT <28, 125>
        POINT <14, 111>
        POINT <14, 110>
        POINT <54, 70>
        POINT <54, 69>
        POINT <14, 29>
        POINT <14, 28>

Cool! :bgrin: I wonder, though: your X has just 12 vertices, but you define 24. Was there a problem with 12?
Title: Re: Polygon Example
Post by: zedd151 on March 06, 2025, 09:46:23 AM
Quote from: _japheth on March 06, 2025, 09:41:58 AM
Quote from: zedd151 on March 06, 2025, 04:52:36 AM    xpoints label dword ; array of point structs
        POINT <28, 14>
        POINT <29, 14>
        POINT <69, 54>
        POINT <70, 54>
        POINT <110, 14>
        POINT <111, 14>
        POINT <125, 28>
        POINT <125, 29>
        POINT <85, 69>
        POINT <85, 70>
        POINT <125, 110>
        POINT <125, 111>
        POINT <111, 125>
        POINT <110, 125>
        POINT <70, 85>
        POINT <69, 85>
        POINT <29, 125>
        POINT <28, 125>
        POINT <14, 111>
        POINT <14, 110>
        POINT <54, 70>
        POINT <54, 69>
        POINT <14, 29>
        POINT <14, 28>

Cool! :bgrin: I wonder, though: your X has just 12 vertices, but you define 24. Was there a problem with 12?

I wanted the polygon to be pixel-wise identical to a bitmap, that's how many data points I had gotten by examining the bitmap.  :smiley:

If you notice there are some pairs of points that are only different by 1 pixel, either on the x axis or y. None of the corners are sharp corners.  :icon_idea:

I did not try to reduce the data point count.
Title: Re: Polygon Example
Post by: NoCforMe on March 06, 2025, 10:22:11 AM
I'd like to see what it looks like with only the necessary 12 vertices. Seems like sharp corners is what you want here anyhow.
Title: Re: Polygon Example
Post by: zedd151 on March 06, 2025, 10:36:17 AM
Quote from: NoCforMe on March 06, 2025, 10:22:11 AMI'd like to see what it looks like with only the necessary 12 vertices. Seems like sharp corners is what you want here anyhow.
Examine very closely this png image. Look exactly where the pixels are, at the percieved corners.
I said I examined the original bitmap to obtain EXACTLY, the points (x and y coordinates) necessary to acheive the desired goal. That is replicating exactly, the original bitmap image.

Here is a png image showing only the outline of the "X"! You can see more clearly by looking at the outline.
(https://i.postimg.cc/gJmbGWZw/x.png)

Here is the original filled bitmap. PostImage converted the .bmp to png, but is still pixel per pixel accurate.
(https://i.postimg.cc/3wwb1xmF/x.png)

Notice no sharp corners? If it had sharp corners, then yes it would only have 12 vertices.

You could probaly make one with 12 vertices, but since the height and width of mine is divisible exactly by 2 (even numer of pixels), mine could never be and still be centered properly.
Yours with 12 vertices, would have height and width with an odd number of pixels, if you had created one with 12 vertices and centered it on a rectangle with dimensions divisible by 2, it would be off center by 1 pixel on both axis.

Title: Re: Polygon Example
Post by: NoCforMe on March 06, 2025, 10:53:35 AM
Images are too small to really see the corners.
To my eyes, they look plenty sharp.

Could you code it up with 12 vertices and see what it looks like?
Title: Re: Polygon Example
Post by: zedd151 on March 06, 2025, 10:58:21 AM
Quote from: NoCforMe on March 06, 2025, 10:53:35 AMImages are too small to really see the corners.
To my eyes, they look plenty sharp.

Could you code it up with 12 vertices and see what it looks like?
No. It has 24 vertices beacuase the dimensions are exactly divisible by 2.

If I were to make one with 12 vertices, it would be off center by one pixel due to having dimesions that are an odd number, and not divisible by 2. Not what I was after.
If you are worried about wasting precious data... don't be, computer memory is plentiful.  :tongue:

Yes, the image I produced using a polygon with 24 vertices appears sharp enough.

The example posted was to show the simplicity of using a polygon, in place of a bitmap, or drawing separate lines then filling the resulting shape.
It was never an example of using the least amount of data points (or vertices). Sheesh!
I thought that newcomers to gdi graphics related code (or anyone else for that matter) might find it useful, so I posted my code to share it.
Title: Re: Polygon Example
Post by: _japheth on March 06, 2025, 06:24:30 PM
Magnifying the image by 4 clearly shows the "soft" edges:

X.PNG
Title: Re: Polygon Example
Post by: zedd151 on March 07, 2025, 12:39:08 AM
Yes indeed, _japheth.  :thup:

I should have been more articulate in my first reply to you. That would have negated the need for further discussion about vertices. It has to do with the math behind the plotting of the pixels.
If the resultant image dimensions were to be divisible by 2, twice as many vertices would be needed = 24 to draw the polygon. If the dimensions were not divisible by 2 (i.e., an odd number), then only 12 vertices would be needed to draw the same polygon.
Same rules apply to a diamond shaped ( think of a square rotated by +-45 degrees) polygon, or any other polygon that has lines that are on a +- 45 degree angle from being plumb or square, and the dimension are to be divisible by 2 exactly.

To the naked eye and at normal unaided resolution, the image appears to have only 12 vertices and appears sharp cornered, even though it actually has 24 and the  corners are not quite sharp.  :smiley:

Sorry _japheth, my original reply here I called you "sinsi". I don't know why that happened. (brain fart?) No offense intended to either yourself or sinsi.
Title: Re: Polygon Example
Post by: jj2007 on March 07, 2025, 07:15:54 AM
Quote from: NoCforMe on March 06, 2025, 10:22:11 AMI'd like to see what it looks like with only the necessary 12 vertices
(https://i.postimg.cc/R3DnbbSr/X.png) (https://postimg.cc/R3DnbbSr)


include \masm32\MasmBasic\Res\MbGui.asm ; src
  MakePen hPen, RgbCol(255, 255, 0, 0), width 18, startarrow LineCapRound
  MakePath MyPoly, Polygon(280:140, 690:540, 1100:140, 1250:280, 850:690, 1250:1100, 1110:1250, 700:850, 290:1250, 140:1110, 540:700, 140:290) ; width:height
  MakeBrush hBrush, RgbCol(200, 255, 0, 0) ; ARGB: 200 is transparency
  invGdip GdipSetPenLineJoin, hPen, LineJoinRound

Event Paint
  GuiDraw MyPoly, hPen/hBrush, 0, 0, 1200.0, 1500.0 ; Gdi+: use a pen/brush, w+h, scale x, y
EndOfCode
Title: Re: Polygon Example
Post by: NoCforMe on March 07, 2025, 09:13:17 AM
Yabbut JJ, you selected round corners:
invGdip GdipSetPenLineJoin, hPen, LineJoinRoundCan we see it with sharp corners?
Title: Re: Polygon Example
Post by: jj2007 on March 07, 2025, 09:40:52 AM
Just comment out the GdipSetPenLineJoin...

The point is that the 24 vertex version does a bit of rounding, nothing else.
Title: Re: Polygon Example
Post by: zedd151 on March 08, 2025, 06:13:22 AM
Quote from: NoCforMe on March 06, 2025, 10:22:11 AMI'd like to see what it looks like with only the necessary 12 vertices. Seems like sharp corners is what you want here anyhow.
Quote from: NoCforMe on March 07, 2025, 09:13:17 AMCan we see it with sharp corners?

Just for you. (Magnified 4 times) The Red X with 24 vertices, with a gray X having 12 vertices superimposed on top of it.
Centering lines are in bright green.

You can see here, that the 12 vertex gray image is not perfectly centered - it is off center by 1 pixel in x and y axes, just as I mentioned above that it would be. Not good enough for my purposes.
(https://i.postimg.cc/SNfBT11S/demo.png)

Now you can make that 12 vertex gray image larger so as to be centered, but the 45 degree diagonal  lines will not be straight. And if you do manage to get straight 45 degree diagonal lines, the image will not be symmetrical (+- 90 degrees, +-180 degrees, etc). Test this for yourself in mspaint.

Sometimes, you just have to accept that certain things are done for a reason whether you agree with that reason or no. It doesn't mean it is due to any ineptitude or lack of knowledge or understanding. Not everything needs to be questioned or challenged, imo.

This thread was intended to demonstrate that creating a polygon is very easy to do. Something that I just recently found out, since I had never needed to do it before.
Should I now have the irrelevent replies deleted, or moved to their own topic, after my reply HERE (https://masm32.com/board/index.php?msg=136613)? That reply should have answered _japheths question sufficiently and I believe it did, as well as addressed your concerns as well.

Thank you for turning another thread into a 3 ring polygon circus.  :undecided:
Title: Re: Polygon Example
Post by: NoCforMe on March 08, 2025, 07:00:02 AM
Oh, c'mon, don't act so butthurt, Zedd. What do you want, total thread purity?
It's all part of a discussion. All's fair in love and coding.
Title: Re: Polygon Example
Post by: zedd151 on March 08, 2025, 07:05:05 AM
This is the Campus, not an area for a free for all.

Is there anything wrong with my code?  If not, everything else here is just noise after the first three posts.
Title: Re: Polygon Example
Post by: NoCforMe on March 08, 2025, 07:44:10 AM
No, there's nothing wrong with your code.
So please just chill out.