News:

Masm32 SDK description, downloads and other helpful links
Message to All Guests
NB: Posting URL's See here: Posted URL Change

Main Menu

Polygon Example

Started by zedd151, March 06, 2025, 04:52:36 AM

Previous topic - Next topic

zedd151

: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:


    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: You cannot view this attachment.

¯\_(ツ)_/¯   :azn:

'As we don't do "requests", show us your code first.'  -  hutch—

_japheth

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?
Dummheit, gepaart mit Dreistigkeit - eine furchtbare Macht.

zedd151

#2
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.
¯\_(ツ)_/¯   :azn:

'As we don't do "requests", show us your code first.'  -  hutch—

NoCforMe

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.
Assembly language programming should be fun. That's why I do it.

zedd151

#4
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.


Here is the original filled bitmap. PostImage converted the .bmp to png, but is still pixel per pixel accurate.


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.

¯\_(ツ)_/¯   :azn:

'As we don't do "requests", show us your code first.'  -  hutch—

NoCforMe

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?
Assembly language programming should be fun. That's why I do it.

zedd151

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.
¯\_(ツ)_/¯   :azn:

'As we don't do "requests", show us your code first.'  -  hutch—

_japheth

Magnifying the image by 4 clearly shows the "soft" edges:

You cannot view this attachment.
Dummheit, gepaart mit Dreistigkeit - eine furchtbare Macht.

zedd151

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.
¯\_(ツ)_/¯   :azn:

'As we don't do "requests", show us your code first.'  -  hutch—

jj2007

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



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

NoCforMe

Yabbut JJ, you selected round corners:
invGdip GdipSetPenLineJoin, hPen, LineJoinRoundCan we see it with sharp corners?
Assembly language programming should be fun. That's why I do it.

jj2007

Just comment out the GdipSetPenLineJoin...

The point is that the 24 vertex version does a bit of rounding, nothing else.

zedd151

#12
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.


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? 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:
¯\_(ツ)_/¯   :azn:

'As we don't do "requests", show us your code first.'  -  hutch—

NoCforMe

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.
Assembly language programming should be fun. That's why I do it.

zedd151

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.
¯\_(ツ)_/¯   :azn:

'As we don't do "requests", show us your code first.'  -  hutch—