Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Help needed regarding Lesson 17
#11
LOL, I was just beginning work on a right angle visual. Thanks bplus!
New to QB64pe? Visit the QB64 tutorial to get started.
QB64 Tutorial
Reply
#12
(10-08-2024, 07:04 PM)SMcNeill Wrote:
Quote: Pouli: So
angle = _Atan2(y2 - y1, x2 - x1) * 180 / PI ?
The result is still off

Feel free to pop into Discord and chat with the guy.

It may be simply a case of him getting the proper result and then needing to rotate/flip it until it fits into the coordinate system Terry is using in his tutorial. I dunno. All I know is he's saying it's not working, even after the Y and X swap as you (and Rho) both pointed out for him.

Quote:Maybe I misunderstand something totally. The angle should be around 190, give or take, right?

How does he respond to the diagram in my reply #7 because that diagram jives with all the Basic Trig Functions.

My Discord access gets me another set of Basic and QB64 stuff, sorry.

@SMcNeill Oops sorry I meant reply #4 diagram.
b = b + ...
Reply
#13
Terry might have everything turned counter clockwise 90 degrees to make 0 degrees North. 283 -90 = 193 what he thinks it should be.
b = b + ...
Reply
#14
(10-08-2024, 07:22 PM)bplus Wrote: Terry might have everything turned counter clockwise 90 degrees to make 0 degrees North. 283 -90 = 193 what he thinks it should be.

0 is North.  90 is East.  

I'm thinking he just needs to subtract 90 to rotate his angle to what he thinks it should be, but then he tells me odd things like: 

(0, 1) would be 180 degrees.
(1, 0) would be 270 degrees
(1, 1) would be 135 degrees.

And that seems inverted/flipped to me, so I have no idea how his coordinate system is laid out in regards to the angle.  Tongue
I don't know if he's getting the right answers and thinking them wrong, or if it's the right answer just not turned/flipped the match the coordinates, or if it's just something wrong to completely begin with.

(And that's basically up to where we're at on the Discord discussion, and why Rho posted the issue here so perhaps someone else could help look over it and see what's what.)
Reply
#15
Here it is in Basic coordinate system
Code: (Select All)
'(0, 1) would be 180 degrees.
'(1, 0) would be 270 degrees
'(1, 1) would be 135 degrees.

' assuming from (0, 0) on all these?

'(0,0) is top, left corner of Basic screen

' instead of 1 let's say 100

' (0,100) in blue
Screen 12
Color 9
Line (0, 0)-(0, 100)
Circle (0, 100), 2
Locate 12, 1: Print _R2D(_Atan2(100, 0))

' (100, 0) in yellow
Color 14
Line (0, 0)-(100, 0)
Circle (100, 0), 2
Locate 13, 1: Print _R2D(_Atan2(0, 100))

' (100, 100) in red
Color 12
Line (0, 0)-(100, 100)
Circle (100, 100), 2
Locate 14, 1: Print _R2D(_Atan2(100, 100))


Attached Files Image(s)
   
b = b + ...
Reply
#16
Looking over Lesson 17

This from Degrees and Radians is first step in wrong direction:
   

This system that puts 0 Degrees in North Direction will go against all the Basic Trig functions specially _ATan2 that has 0 degrees in the East Direction. I have already provided 2 visuals for the actual positioning of degrees and directions.


And in all due respect this visual:
   

Is a freak'n nitemare! No wonder that guy is confused

To move from point 1 = x1, y1 to point 2 = x2, y2
dx = x2-x1
dy = y2-y1
Angle = _Atan2(dy, dx) in radians

to normalize we use
d = _hypot(dx, dy)
Vx = dx/d x component of vector
Vy = dy/d y component of vector

Drawing Regular Polygons with different number of sides is excellect teaching device for Sin and Cos.

I will make up a lesson for you. Wait maybe you @TerryRitchie already have that somewhere?
b = b + ...
Reply
#17
Already had it done when I converted Trig functions to Degrees:
Code: (Select All)
Option _Explicit
_Title "Degrees for Regular Polygon" 'b+ 2022-10-12
Screen _NewImage(800, 600, 32) ' standard screen size 800 wide, 600 height for quick QB64 Demos with full color potential (the 32)
_ScreenMove 250, 50
Dim cx, cy, radius, degrees, x, y, xOffset, yOffset, dial&, dStart, NPoints, secDegrees, p, saveX, saveY
cx = _Width / 2 ' middle of the screen point  center x
cy = _Height / 2 ' center y
radius = 250 ' max is 300 for height 600
ArrowTo cx, cy, 0, radius - 3, &HFFFFFFFF
For degrees = 0 To 359 Step 10 ' go around a full circle in degrees in steps of 10 degrees

    ' calculate and draw points around the center of the screen
    x = cx + radius * CosD(degrees) ' use CosD for x dimensions
    y = cy + radius * SinD(degrees) ' use SinD for y dimensions
    Circle (x, y), 1 ' draw bigger points than single pixel

    ' labeling the degree angles before or after the point ?
    If x < cx Then xOffset = -10 * Len(_Trim$(Str$(degrees))): yOffset = 0
    If x > cx Then xOffset = 4 * Len(_Trim$(Str$(degrees))): yOffset = 0
    If x = cx Then
        xOffset = -4 * Len(_Trim$(Str$(degrees)))
        If y > cy Then yOffset = 20 Else yOffset = -20
    End If
    _PrintString (x + xOffset, y - 8 + yOffset), _Trim$(Str$(degrees))
Next

' save our compass dial to image
dial& = _NewImage(_Width, _Height, 32)
_PutImage , 0, dial& ' screen to dial image stored

' A look at Regular Polygons about the Center of the Screen,
' say they all should start at 270 Degrees so they all point North
dStart = 270
For NPoints = 3 To 12
    secDegrees = 360 / NPoints ' how many degree is each section of poly eg triangle = 120 degree
    Cls
    _PutImage , dial&, 0
    For p = 1 To NPoints
        x = cx + radius * CosD(dStart + p * secDegrees)
        y = cy + radius * SinD(dStart + p * secDegrees)
        If p = 1 Then PSet (x, y), &HFFFFFF00: saveX = x: saveY = y Else Line -(x, y), &HFFFFFF00
    Next
    Line -(saveX, saveY), &HFFFFFF00 ' back to first point
    Print "N points ="; NPoints; "  Section angle ="; secDegrees; " degrees,   zzz... press any for next polygon or end..."
    _Display ' stop the blinking
    Sleep '             .
Next

' use angles in degrees units instead of radians (converted inside sub)
Function CosD (degrees)
    ' Note this function uses whatever the default type is, better not be some Integer Type.
    CosD = Cos(_D2R(degrees))
End Function

' use angles in degrees units instead of radians (converted inside sub)
Function SinD (degrees)
    ' Note this function uses whatever the default type is, better not be some Integer Type.
    SinD = Sin(_D2R(degrees))
End Function

' use angles in degrees units instead of radians (converted inside sub)
Function DAtan2 (x1, y1, x2, y2) ' The angle in degrees a 2nd point (x2, y2)  makes to a first point (x1, y1)
    ' Note this function uses whatever the default type is, better not be some Integer Type.
    ' Delta means change between 1 measure and another for example x2 - x1
    Dim deltaX, deltaY, rtn
    deltaX = x2 - x1
    deltaY = y2 - y1
    '  To find the angle point(x2, y2) makes to (x1, y1) in Degrees
    ' Take DegreeAngle = DAtan2(y2 - y1, x2 - x1)
    rtn = _R2D(_Atan2(deltaY, deltaX))
    If rtn < 0 Then DAtan2 = rtn + 360 Else DAtan2 = rtn
End Function

' use angles in degrees units instead of radians (converted inside sub)
Sub ArrowTo (BaseX As Long, BaseY As Long, dAngle As Double, lngth As Long, colr As _Unsigned Long)
    Dim As Long x1, y1, x2, y2, x3, y3
    Dim As Double rAngle
    rAngle = _D2R(dAngle)
    x1 = BaseX + lngth * Cos(rAngle)
    y1 = BaseY + lngth * Sin(rAngle)
    x2 = BaseX + .8 * lngth * Cos(rAngle - _Pi(.05))
    y2 = BaseY + .8 * lngth * Sin(rAngle - _Pi(.05))
    x3 = BaseX + .8 * lngth * Cos(rAngle + _Pi(.05))
    y3 = BaseY + .8 * lngth * Sin(rAngle + _Pi(.05))
    Line (BaseX, BaseY)-(x1, y1), colr
    Line (x1, y1)-(x2, y2), colr
    Line (x1, y1)-(x3, y3), colr
End Sub

' use angles in degrees units instead of radians (converted inside sub)
Sub drawArc (xc, yc, radius, dStart, dMeasure, colr As _Unsigned Long)
    ' xc, yc Center for arc circle
    ' rStart is the Radian Start Angle, use _D2R for conversion from Degrees to Radians
    ' rMeasure is the measure of Arc in Radain units, use _D2R for conversion from Degrees to Radians
    ' Arc will start at rStart and go clockwise around for rMeasure Radians

    Dim rStart, rMeasure, rEnd, stepper, a, x, y
    rStart = _D2R(dStart)
    rMeasure = _D2R(dMeasure)
    rEnd = rStart + rMeasure
    stepper = 1 / radius ' the bigger the radius the smaller  the steps
    For a = rStart To rEnd Step stepper
        x = xc + radius * Cos(a)
        y = yc + radius * Sin(a)
        If a > rStart Then Line -(x, y), colr Else PSet (x, y), colr
    Next
End Sub
b = b + ...
Reply
#18
(10-09-2024, 01:25 PM)bplus Wrote: Looking over Lesson 17

This from Degrees and Radians is first step in wrong direction:


This system that puts 0 Degrees in North Direction will go against all the Basic Trig functions specially _ATan2 that has 0 degrees in the East Direction. I have already provided 2 visuals for the actual positioning of degrees and directions.


And in all due respect this visual:


Is a freak'n nitemare! No wonder that guy is confused

To move from point 1 = x1, y1 to point 2 = x2, y2
  dx = x2-x1
  dy = y2-y1
Angle = _Atan2(dy, dx) in radians

to normalize we use
d = _hypot(dx, dy)
Vx = dx/d  x component of vector
Vy = dy/d  y component of vector

Drawing Regular Polygons with different number of sides is excellect teaching device for Sin and Cos.

I will make up a lesson for you. Wait maybe you @TerryRitchie already have that somewhere?
LOL, yeah, well, I asked for help when I was beginning lesson 17 way back in early 2022 however no one had the time to help. When I finished it I asked those with math inclinations to look it over for correctness. Again, nothing, so I figured what I had was good. I am completely self taught when it comes to math.

What is happening here is my confusion over mixing the need to orientate everything to match the screen versus the correct orientation. I've gotten much better over the past 2.5 years and yes, I see the issues you have pointed out.

I would gladly welcome help to correct this lesson.
New to QB64pe? Visit the QB64 tutorial to get started.
QB64 Tutorial
Reply
#19
I do believe I warned about moving 0 degrees when you asked two years ago, in fact that is when I started these code examples. (I will look it up)

Here is code mod from today, I move a yellow box calculated from the Vx, Vy which is using the mouse and to set
Vx = velocity * CosD(MouseAngleDegrees) ' velocity = 10 pixels per move
Vy = 10 * SinD(MouseAngleDegrees)

It's all in degrees to save us all from Radians Smile
Code: (Select All)
' OK let's be rid on the confusion caused by Radians
' by using User Defined Funcions SinD and CosD that take Degrees 0 to 360 for whole circle,
' and having replaced _Atan2 by returning an angle in Degrees between 2 points: DAtan2(deltaY, deltaX)

_Title "Degrees for everything, Press key to Move Square 10 pixels at Mouse Angle " 'b+ 2024-10-09
Screen _NewImage(800, 600, 32) ' standard screen size 800 wide, 600 height for quick QB64 Demos with full color potential (the 32)
_ScreenMove 250, 50

cx = _Width / 2 ' middle of the screen point  center x
cy = _Height / 2 ' center y
bx = cx - 5
by = cy - 5
radius = 250 ' max is 300 for height 600
ArrowTo cx, cy, 0, radius - 3, &HFFFFFFFF
For degrees = 0 To 359 Step 10 ' go around a full circle in degrees in steps of 10 degrees

    ' calculate and draw points around the center of the screen
    x = cx + radius * CosD(degrees) ' use CosD for x dimensions
    y = cy + radius * SinD(degrees) ' use SinD for y dimensions
    Circle (x, y), 1 ' draw bigger points than single pixel

    ' labeling the degree angles before or after the point ?
    If x < cx Then Xoffset = -10 * Len(_Trim$(Str$(degrees))): YOffset = 0
    If x > cx Then Xoffset = 4 * Len(_Trim$(Str$(degrees))): YOffset = 0
    If x = cx Then
        Xoffset = -4 * Len(_Trim$(Str$(degrees)))
        If y > cy Then YOffset = 20 Else YOffset = -20
    End If
    _PrintString (x + Xoffset, y - 8 + YOffset), _Trim$(Str$(degrees))
Next

' save our compass dial to image
dial& = _NewImage(_Width, _Height, 32)
_PutImage , 0, dial& ' screen to dial image stored

' Getting use to seeing angles mouse makes to center of screen
Do
    Cls
    _PutImage , dial&, 0
    While _MouseInput: Wend ' this checks where mouse is right now!
    mx = _MouseX: my = _MouseY: mb1 = _MouseButton(1) ' left mouse down  ' saves mouse status to common variable names
    'lets's the angle in degrees the mouse is to the center of the screen
    dAngle = DAtan2(cx, cy, mx, my)
    Print "The mouse pointer is "; _Trim$(Str$(dAngle)); " Degrees from the screen center." ' then center point is first the mouse point is second
    ArrowTo cx, cy, dAngle, radius - 3, &HFFFFFF00
    drawArc cx, cy, 70, 0, dAngle, &HFFFFFF00

    'Move  on any keypress
    If InKey$ <> "" Then  ' EDIT: fixed so formula for normalized Vx, Vy is used
        Vx = CosD(dAngle) ' 10 is pixels per move Vx = pixels along x-axis
        Vy = SinD(dAngle) ' 10 is pixels per move Vy = pixels along y-axis
        bx = bx + 10 * Vx
        by = by + 10 * Vy
    End If
    Line (bx, by)-Step(10, 10), &HFFFFFF00, BF ' draw square

    _Display ' stop the blinking
    _Limit 60 ' only loop 60 times per second
Loop Until _KeyDown(27)


' use angles in degrees units instead of radians (converted inside sub)
Function CosD (degrees)
    ' Note this function uses whatever the default type is, better not be some Integer Type.
    CosD = Cos(_D2R(degrees))
End Function

' use angles in degrees units instead of radians (converted inside sub)
Function SinD (degrees)
    ' Note this function uses whatever the default type is, better not be some Integer Type.
    SinD = Sin(_D2R(degrees))
End Function

' use angles in degrees units instead of radians (converted inside sub)
Function DAtan2 (x1, y1, x2, y2) ' The angle in degrees a 2nd point (x2, y2)  makes to a first point (x1, y1)
    ' Note this function uses whatever the default type is, better not be some Integer Type.
    ' Delta means change between 1 measure and another for example x2 - x1
    deltaX = x2 - x1
    deltaY = y2 - y1
    '  To find the angle point(x2, y2) makes to (x1, y1) in Degrees
    ' Take DegreeAngle = DAtan2(y2 - y1, x2 - x1)
    rtn = _R2D(_Atan2(deltaY, deltaX))
    If rtn < 0 Then DAtan2 = rtn + 360 Else DAtan2 = rtn
End Function

' use angles in degrees units instead of radians (converted inside sub)
Sub ArrowTo (BaseX As Long, BaseY As Long, dAngle As Double, lngth As Long, colr As _Unsigned Long)
    Dim As Long x1, y1, x2, y2, x3, y3
    Dim As Double rAngle
    rAngle = _D2R(dAngle)
    x1 = BaseX + lngth * Cos(rAngle)
    y1 = BaseY + lngth * Sin(rAngle)
    x2 = BaseX + .8 * lngth * Cos(rAngle - _Pi(.05))
    y2 = BaseY + .8 * lngth * Sin(rAngle - _Pi(.05))
    x3 = BaseX + .8 * lngth * Cos(rAngle + _Pi(.05))
    y3 = BaseY + .8 * lngth * Sin(rAngle + _Pi(.05))
    Line (BaseX, BaseY)-(x1, y1), colr
    Line (x1, y1)-(x2, y2), colr
    Line (x1, y1)-(x3, y3), colr
End Sub

' use angles in degrees units instead of radians (converted inside sub)
Sub drawArc (xc, yc, radius, dStart, dMeasure, colr As _Unsigned Long)
    ' xc, yc Center for arc circle
    ' rStart is the Radian Start Angle, use _D2R for conversion from Degrees to Radians
    ' rMeasure is the measure of Arc in Radain units, use _D2R for conversion from Degrees to Radians
    ' Arc will start at rStart and go clockwise around for rMeasure Radians

    Dim rStart, rMeasure, rEnd, stepper, a, x, y
    rStart = _D2R(dStart)
    rMeasure = _D2R(dMeasure)
    rEnd = rStart + rMeasure
    stepper = 1 / radius ' the bigger the radius the smaller  the steps
    For a = rStart To rEnd Step stepper
        x = xc + radius * Cos(a)
        y = yc + radius * Sin(a)
        If a > rStart Then Line -(x, y), colr Else PSet (x, y), colr
    Next
End Sub

Any keypress moves yellow square in direction of mouse angle 10 pixels.
b = b + ...
Reply
#20
OMG I found it!
https://qb64phoenix.com/forum/showthread...76#pid5876

And I did try again, it's not arbitrary where you can put 0 degrees... assuming if you want the Trig functions to work as they are.
https://qb64phoenix.com/forum/showthread...81#pid5881

You did seem extremely invested in all the code you had written and I can understand reluctance to redo everything.

Plus... vectors! and radians! and trig! oh my! Smile

@TerryRitchie is reply #19 a help, moves yellow box in direction of mouse arrow using... damn Vx, Vy weren't normalized
normalize would be
Vx = Cos(MouseAngleDegrees) 'before I multiplied by 10
Vy = Sin(MouseAngleDegrees) ' before I multiplied by 10
That makes them <=1 and >= -1
EDIT: fixed in code above
b = b + ...
Reply




Users browsing this thread: 3 Guest(s)