Formula To VBA Help (Burnt out on a Monday!) (1 Viewer)

BlueIshDan

☠
Local time
Today, 13:09
Joined
May 15, 2014
Messages
1,122
Here it goes. I'm working with multiple coordinate systems with ouR engineering data here at work. I need to select an area in an outer scope grid (Main grid) and get the x, y values in the sub coordinate systems.

I have the formula I just can't wake up enough to get it right in my code.
If anyone could help figure this out with me I'd be SO grateful! :)

Here is what the Formula is.


Here is what my code is as of now. Don't mind the variants I just haven't seen the behaviour of the numbers yet, it will be done properly later on.

Code:
Option Explicit

Const MAIN_MAX_X As Integer = 11600
Const MAIN_MAX_Y As Integer = 10000

Private Sub rectCoordinateScope_MouseMove(Button As Integer, Shift As Integer, x As Single, Y As Single)
    
    Dim x_percent As Variant: x_percent = Round(x / rectCoordinateScope.Width, 2)
    Dim y_percent As Variant: y_percent = Round(Y / rectCoordinateScope.Height, 2)
    
    lblMainX.Caption = MAIN_MAX_X * x_percent
    lblMainY.Caption = MAIN_MAX_Y * y_percent
    
    txtSouthGridX.Value = ConvertX(1, 0, CLng(lblMainX.Caption), CLng(lblMainY.Caption), 2000)
    txtSouthGridY.Value = ConvertY(1, 0, CLng(lblMainX.Caption), CLng(lblMainY.Caption), 2000)
    
End Sub

Public Function ConvertX(ByVal coordinate_scale As Variant, _
                         ByVal rotation As Variant, _
                         ByVal from_x As Variant, _
                         ByVal from_y As Variant, _
                         ByVal translation_x As Variant) As Variant
                         
    ConvertX = Sqr(coordinate_scale * (from_x * Cos(0) - from_y * Sin(0)) + translation_x)
    
End Function

Public Function ConvertY(ByVal coordinate_scale As Variant, _
                         ByVal rotation As Variant, _
                         ByVal from_x As Variant, _
                         ByVal from_y As Variant, _
                         ByVal translation_y As Variant) As Variant
                         
    ConvertY = Sqr(coordinate_scale * (from_x * Cos(0) + from_y * Sin(0)) + translation_y)
    
End Function
 

Attachments

  • CoordinateConversion.png
    CoordinateConversion.png
    35.6 KB · Views: 262

vbaInet

AWF VIP
Local time
Today, 17:09
Joined
Jan 22, 2010
Messages
26,374
You'll need a Maths genius who understands your formula to help you with this one ;) Which I'm not :)

Anyway, it's seems apparent what your calculations are there. Although the compiler should do the multiplications first additions and subtractions follow, I would be inclined to bracket properly. I'll take the first one (i.e. ConvertX) as an example:
Code:
ConvertX = Sqr((coordinate_scale * ((from_x * Cos(0)) - (from_y * Sin(0)))) + translation_x)
Always good to put those extra brackets. Compare the result of that to what you had before.
 

vbaInet

AWF VIP
Local time
Today, 17:09
Joined
Jan 22, 2010
Messages
26,374
And another thing, are you sure you should be casting those captions to Long instead of Decimal or Double?
 

BlueIshDan

☠
Local time
Today, 13:09
Joined
May 15, 2014
Messages
1,122
Thank you very much for your reply vbaInet!
Yes, It was converted into whole numbers.
 

BlueIshDan

☠
Local time
Today, 13:09
Joined
May 15, 2014
Messages
1,122
Blah, just learned that the SouthGrid Coordinate System is used as the base coordinate system here -.-

Undocumented of course.
 

BlueIshDan

☠
Local time
Today, 13:09
Joined
May 15, 2014
Messages
1,122
The main scope was just an imaginary grid that I made.
I made that grid to place the 3 sub grid systems into.

From what I know now. There is no containing grid system. The 3 systems are relative to each other. Here is a chart I found in documentation.



 

Attachments

  • Capture2.PNG
    Capture2.PNG
    31 KB · Views: 228
  • Capture3.PNG
    Capture3.PNG
    55.3 KB · Views: 218

vbaInet

AWF VIP
Local time
Today, 17:09
Joined
Jan 22, 2010
Messages
26,374
I'm not following. Of course you understand this stuff more than myself, so if you can explain in layman's terms that would be great :)

How does this new find affect your code?
 

BlueIshDan

☠
Local time
Today, 13:09
Joined
May 15, 2014
Messages
1,122
Well, what the formula does is converts one coordinates systems x,y to another's.

My initial thought was that the three coordinate systems were all contained in one large grid. Then I came to realize that they're not and they all just relate to each other.

This will not affect the code, It will simply affect the results :).
 

vbaInet

AWF VIP
Local time
Today, 17:09
Joined
Jan 22, 2010
Messages
26,374
Meaning you didn't need to do the conversion in the first place? :)
 

BlueIshDan

☠
Local time
Today, 13:09
Joined
May 15, 2014
Messages
1,122
No meaning I was converting using the wrong values. I have yet to test this new understanding. Got into something at work lol. No more exploring time for now :(
 

BlueIshDan

☠
Local time
Today, 13:09
Joined
May 15, 2014
Messages
1,122
edited to move to new thread since this one is dead.
 
Last edited:

pr2-eugin

Super Moderator
Local time
Today, 17:09
Joined
Nov 30, 2011
Messages
8,494
I would approve !

EDIT: Burn out Wednesday ! Lol
 
Last edited:

BlueIshDan

☠
Local time
Today, 13:09
Joined
May 15, 2014
Messages
1,122
Just realized I had forgot to invert the Cos and Sin when converting the Y value.
Also relized I was putting 0 into the Cos and Sin instead of the rotation.
(burnt out week I guess)
 

vbaInet

AWF VIP
Local time
Today, 17:09
Joined
Jan 22, 2010
Messages
26,374
I thought you were representing theta as zero. Obviously not then.
 

Users who are viewing this thread

Top Bottom