# Thread: why Polygon() don't use the brush?

1. ## why Polygon() don't use the brush?

i change the brush:
Code:
```Dim oldbrush As Long
oldbrush = GetDCBrushColor(Me.hDC)
SetDCBrushColor Me.hDC, RGB(255, 0, 0)```
i call the Polygon() function by double a double vectors(draw 1 line at a time)...
Code:
```Private Sub DrawLine(Origin As Position3D, Destiny As Position3D, Rotation As Angle3D, WorldSize As Size3D)
Dim Points(2) As POINTAPI
Dim NewPosition3D(2) As Position3D
Dim RotatedPosition As Position3D

With Player1.Position
FillPosition3D RotatedPosition, .x, .Y, .Z  'rotate using camera Position
End With

'Line:
'Calculate Vector1:
FillPosition3D NewPosition3D(0), Origin.x, Origin.Y, Origin.Z
NewPosition3D(0) = Rotate(NewPosition3D(0), Rotation, RotatedPosition)

'Calculate Vector2:
FillPosition3D NewPosition3D(1), Destiny.x, Destiny.Y, Destiny.Z
NewPosition3D(1) = Rotate(NewPosition3D(1), Rotation, RotatedPosition)

'Testing If Vector0 and Vector1 are inside of camera:

If (IsOnCamera(NewPosition3D(0), camera1.Position, camera1.size) = False And _
IsOnCamera(NewPosition3D(1), camera1.Position, camera1.size) = False) Then
Exit Sub
End If

If (IsOnCamera(NewPosition3D(0), camera1.Position, camera1.size) = False) Then
NewPosition3D(0) = GetInCamVector(NewPosition3D(0), NewPosition3D(1))
End If

If (IsOnCamera(NewPosition3D(1), camera1.Position, camera1.size) = False) Then
NewPosition3D(1) = GetInCamVector(NewPosition3D(1), NewPosition3D(0))
End If

'Convert the Vector1 to 2D:
Points(0) = ConvertPositon3DTo2D(NewPosition3D(0), camera1.size)

'Convert the Vector2 to 2D:
Points(1) = ConvertPositon3DTo2D(NewPosition3D(1), camera1.size)
Polygon Me.hDC, Points(0), 2

End Sub```
Code:
```DrawLine position1, position2, Rotation, WorldSize
SetDCBrushColor Me.hDC, oldbrush```
the line is drawed red.. on a pen way...
but why the brush isn't used?
yes if i call the Polygon() for draw all the 4 lines at once, the brush is used... but why not on these way? i selected the brush  Reply With Quote

2. ## Re: why Polygon() don't use the brush?

When you call polygon, it has to be a full polygon, in other words, the function will draw a line from the last point to the first point to close the polygon.

Since you only pass two points to the polygon method, you are drawing a polygon that goes from point 1, to point 2, and then back to point 1 to close it. So the polygon has zero area, and looks like a line.

I don't know if the Windows API has a method that allows building a polygon in pieces. APIs that allow that usually have to have separate call to either start the polygon, and the successive calls to the function collect the points until a End Polygon function is called to have the polygon rendered.

Doing it the way you're doing it, how would the polygon function know that you are done drawing the polygon, so it can use the brush and fill it?

If you want this functionality, then perhaps create your own function or sub that you call, which will build up the points in an array with each call, and then when the condition is met to be done with the polygon, have the method call the polygon function once, passing the collected points to it, to draw the complete polygon.  Reply With Quote

3. ## Re: why Polygon() don't use the brush?

now works fine:
Code:
```Private Sub DrawPlane(position As position3D, size As Size3D, Rotation As Angle3D, WorldSize As Size3D, color As ColorConstants)
Dim RotatedPosition As position3D
With Player1.position
FillPosition3D RotatedPosition, .x, .Y, .Z  'Camera Position
End With

If (IsCollision3D(camera1.position, camera1.size, position, size) = False) Then Exit Sub

Dim position3D(8) As position3D
Dim Position2D(8) As POINTAPI
Dim position3DtoDraw(8) As POINTAPI
Dim PositionsCount As Long
PositionsCount = 0
'Draw the Line:
FillStyle = vbFSSolid
ForeColor = vbRed
Dim oldbrush As Long
oldbrush = GetDCBrushColor(Me.hDC)
SetDCBrushColor Me.hDC, RGB(255, 0, 0)
'Line top:
FillPosition3D position3D(0), position.x, position.Y, position.Z
position3D(0) = Rotate(position3D(0), Rotation, RotatedPosition)
FillPosition3D position3D(1), position.x + size.Width, position.Y, position.Z + size.ZDepth
position3D(1) = Rotate(position3D(1), Rotation, RotatedPosition)

If (IsOnCamera(position3D(0), camera1.position, camera1.size) = False) Then
position3D(0) = GetInCamVector(position3D(0), position3D(1))
End If
If (IsOnCamera(position3D(1), camera1.position, camera1.size) = False) Then
position3D(1) = GetInCamVector(position3D(1), position3D(0))
End If

Position2D(0) = ConvertPositon3DTo2D(position3D(0), camera1.size)
Position2D(1) = ConvertPositon3DTo2D(position3D(1), camera1.size)

If (IsOnCamera(position3D(0), camera1.position, camera1.size) = True And _
IsOnCamera(position3D(1), camera1.position, camera1.size) = True) Then
PositionsCount = PositionsCount + 2
position3DtoDraw(0) = Position2D(0)
position3DtoDraw(1) = Position2D(1)
End If

'Line right:
FillPosition3D position3D(2), position.x + size.Width, position.Y, position.Z + size.ZDepth
position3D(2) = Rotate(position3D(2), Rotation, RotatedPosition)

FillPosition3D position3D(3), position.x + size.Width, position.Y + size.Height, position.Z + size.ZDepth
position3D(3) = Rotate(position3D(3), Rotation, RotatedPosition)
If (IsOnCamera(position3D(2), camera1.position, camera1.size) = False) Then
position3D(2) = GetInCamVector(position3D(2), position3D(3))
End If

If (IsOnCamera(position3D(3), camera1.position, camera1.size) = False) Then
position3D(3) = GetInCamVector(position3D(3), position3D(2))
End If
Position2D(2) = ConvertPositon3DTo2D(position3D(2), camera1.size)
Position2D(3) = ConvertPositon3DTo2D(position3D(3), camera1.size)

If (IsOnCamera(position3D(2), camera1.position, camera1.size) = True And _
IsOnCamera(position3D(3), camera1.position, camera1.size) = True) Then
PositionsCount = PositionsCount + 2
position3DtoDraw(2) = Position2D(2)
position3DtoDraw(3) = Position2D(3)
End If

'Line bottom:
FillPosition3D position3D(4), position.x + size.Width, position.Y + size.Height, position.Z + size.ZDepth
position3D(4) = Rotate(position3D(4), Rotation, RotatedPosition)

FillPosition3D position3D(5), position.x, position.Y + size.Height, position.Z
position3D(5) = Rotate(position3D(5), Rotation, RotatedPosition)
If (IsOnCamera(position3D(4), camera1.position, camera1.size) = False) Then
position3D(4) = GetInCamVector(position3D(4), position3D(5))
End If
If (IsOnCamera(position3D(4), camera1.position, camera1.size) = False) Then
position3D(5) = GetInCamVector(position3D(5), position3D(4))
End If
Position2D(4) = ConvertPositon3DTo2D(position3D(4), camera1.size)
Position2D(5) = ConvertPositon3DTo2D(position3D(5), camera1.size)

If (IsOnCamera(position3D(4), camera1.position, camera1.size) = True And _
IsOnCamera(position3D(5), camera1.position, camera1.size) = True) Then
PositionsCount = PositionsCount + 2
position3DtoDraw(4) = Position2D(4)
position3DtoDraw(5) = Position2D(5)
End If

'Line left:
FillPosition3D position3D(6), position.x, position.Y + size.Height, position.Z
position3D(6) = Rotate(position3D(6), Rotation, RotatedPosition)
FillPosition3D position3D(7), position.x, position.Y, position.Z
position3D(7) = Rotate(position3D(7), Rotation, RotatedPosition)
If (IsOnCamera(position3D(6), camera1.position, camera1.size) = False) Then
position3D(6) = GetInCamVector(position3D(6), position3D(7))
End If
If (IsOnCamera(position3D(7), camera1.position, camera1.size) = False) Then
position3D(7) = GetInCamVector(position3D(7), position3D(6))
End If
Position2D(6) = ConvertPositon3DTo2D(position3D(6), camera1.size)
Position2D(7) = ConvertPositon3DTo2D(position3D(7), camera1.size)

If (IsOnCamera(position3D(6), camera1.position, camera1.size) = True And _
IsOnCamera(position3D(7), camera1.position, camera1.size) = True) Then
PositionsCount = PositionsCount + 2
position3DtoDraw(6) = Position2D(6)
position3DtoDraw(7) = Position2D(7)
End If

Polygon Me.hDC, position3DtoDraw(0), PositionsCount
SetDCBrushColor Me.hDC, oldbrush
End Sub```
but i must review the code for draw only what is on camera... but it's working now.. tomorrow i will do it.
thanks for all  Reply With Quote

4. ## Re: why Polygon() don't use the brush?

if i lose the left line(for exemple), because it's outside the camera, the plane isn't drawed.... how can i fix these?
i must draw all lines   Reply With Quote

5. ## Re: why Polygon() don't use the brush?

Which line is the "left line"?

If you're saying the left line is the vertical line at the "front" of a polygon in 3D, and that vertical line is completely behind the camera, then the lines that connect to the vertical line (the top and bottom lines), should have been shortened, and the vertical left line, would now be between the shortened end points of the top and bottom lines, so that the left line is essentially a new line that is at a greater Z depth, and it doesn't go behind the camera.

The line can be off the screen, and still be in front of the camera position, so drawing should work even if the lines are off screen. It is only when the coordinates go behind the camera that the math will cause artifacts if you try to draw using those coordinates.

You can't treat all the lines the same. Lines that are going into or out of the screen need to be tested for going behind the camera, and if only one of the coordinates of the line is behind the camera, that line has to be shortened. Since the vertical line should be using the same coordinates for its top and bottom as the end coordinates of the top and bottom lines, it shouldn't need to be calculated. When you shorten the top and bottom line, the end points of the vertical line should already have been modified as part of that so you don't have to do anything for the vertical lines if you're using the same coordinates.  Reply With Quote

6. ## Re: why Polygon() don't use the brush?

@joaquim: I am willing to have a look and see if I can help out some how. But, it seems the code you have posted depends on several parts that haven't posted here... Do you need any further help with this?  Reply With Quote

7. ## Re: why Polygon() don't use the brush?

If you think you really want to get involved, the last "full" code post was in post #124 of this thread. There was an update and question in post #125.
Unfortunately, work is intruding on my hobby as of late, so I haven't had the time or inclination to look at this or respond.
I can't remember at this point, (I think my mental abilities are going downhill), whether I ran the code in post #124 or not.  Reply With Quote

8. ## Re: why Polygon() don't use the brush? Originally Posted by passel If you think you really want to get involved, the last "full" code post was in post #124 of this thread. There was an update and question in post #125.
Unfortunately, work is intruding on my hobby as of late, so I haven't had the time or inclination to look at this or respond.
I can't remember at this point, (I think my mental abilities are going downhill), whether I ran the code in post #124 or not.
Passel and Joaquim, this looks like a rather large and complex project. I probably don't want to get too involved here. However, I can give you some advice. In the code you linked to I noticed the following:

Code:
```Private Sub Form_KeyDown(KeyCode As Integer, Shift As Integer)
If (KeyCode = vbKeyEscape) Then End
End Sub

'...

Private Sub Timer1_Timer()
If IsKeyDown(vbKeyEscape) = True Then
End
End If

'...```
Several users here have told me the End statement shouldn't be used as it improperly terminates (crashes) a program. I have tried "Unload Me" instead which works just as fine in your code.

yours,
Peter Swinkels  Reply With Quote

9. ## Re: why Polygon() don't use the brush?

Passel: now i can see the camera position and size, just drawing a rectangle... but i continue with a problem on these function:
Code:
```Private Function IsOnCamera(VerticePosition As Position3D, CameraPosition As Position3D, CameraSize As Size3D) As Boolean
If (((VerticePosition.Z) >= CameraPosition.Z) And ((VerticePosition.Z) <= (CameraPosition.Z + CameraSize.ZDepth)) _
And (((VerticePosition.Y) >= CameraPosition.Y) And ((VerticePosition.Y) <= (CameraPosition.Y + CameraSize.Height)))) Then
'And (((VerticePosition.X) >= CameraPosition.X) And ((VerticePosition.X) <= (CameraPosition.X + CameraSize.Width)))) Then
IsOnCamera = True
Else
IsOnCamera = False
End If
End Function```
yes the 'X' and 'width' are commented, because seems that something is wrong with these function and i don't understand why Peter Swinkels and Passel: yes... my problem is when the line is more big(back and front) than screen... because it can be drawed in a different way. that's why i did 1 function that let me short the line:
Code:
```Private Function Ceil(dInput As Double) As Double
' Ceiling, defined as: the smallest integer that is not smaller than x.
' Works for both positive and negative.
Ceil = Int(dInput): If Ceil <> dInput Then Ceil = Ceil + 1#
End Function

Private Function GetInCamVector(Origin As Position3D, Destiny As Position3D) As Position3D
Dim Steps As Double
Steps = Math.Sqr(Math.Abs(Destiny.X - Origin.X) ^ 2 + Math.Abs(Destiny.Y - Origin.Y) ^ 2 + Math.Abs(Destiny.Z - Origin.Z) ^ 2)
Steps = Ceil(Steps)

Dim increment As Position3D
increment.X = (Destiny.X - Origin.X) / Steps
increment.Y = (Destiny.Y - Origin.Y) / Steps
increment.Z = (Destiny.Z - Origin.Z) / Steps

Dim nextpoint As Position3D
nextpoint = Origin
Dim i As Integer

Dim inter As Position3D
Dim Size As Size3D
For i = 1 To Steps
nextpoint.X = nextpoint.X + increment.X
nextpoint.Y = nextpoint.Y + increment.Y
nextpoint.Z = nextpoint.Z + increment.Z
inter.X = Math.Round(nextpoint.X)
inter.Y = Math.Round(nextpoint.Y)
inter.Z = Math.Round(nextpoint.Z)
If (IsOnCamera(inter, camera1.Position, camera1.Size) = True) Then
GetInCamVector = inter
Exit For
End If
Next i
End Function```
the bad results happen, normally, when i rotate and then move to front.
maybe with time i will learn more how to fix these problem for continue draw the world  Reply With Quote

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•