Vector Class

Aaron Blood

Active Member
Joined
Oct 10, 2002
Messages
485
So whadyall think about this?

It's a tutorial I'm working on.

http://www.xl-logic.com/xl_files/vba/vector_class.zip


Any engineering/science/math folks care to check my writeup and calcs for technical accuracy?

...ideas for other examples? Suggestions?

Somehow I feel like I'm reinventing the wheel with this...
Have you seen anyone attempt something like this before?

Is there a better way to represent a vector? :huh:


I kinda viewed this more as "Excel Discussion" than "Excel Question" so I posted it here.
 

Excel Facts

Can Excel fill bagel flavors?
You can teach Excel a new custom list. Type the list in cells, File, Options, Advanced, Edit Custom Lists, Import, OK
Nice work, Aaron.

One initial comment though is the orientation of your vectors. 0 degrees usually points to the right, 90 degrees up, 180 degrees left, and 270 degress down...
 
Upvote 0
Umm, not on my compass...

But an interesting comment nonetheless.

I know radians work like that in a polar graph. Is that what you're getting at?

Maybe an option to tilt is worth investigating then?

I'm accustomed to the navigational references that equate 0 or 360 to North.
 
Upvote 0
Aaron,

I am disappointed not to be able to add any value to your endeavours, but just want to say that that is exceedingly cool!
 
Upvote 0
Hmph. I never noticed that compasses use 0 degrees as North and rotate clockwise. I always just either used the polar degrees or "15 degrees West of North" or similar.

Maybe an option for "Mathematics" vs "Navigation" or something like that would clear some confusion.

Not sure if it adds value to what you've set out to accomplish, but no discussion of vectors would be complete without at least some sort of dot product and cross product coverage.
 
Upvote 0
Hmph. I never noticed that compasses use 0 degrees as North and rotate clockwise. I always just either used the polar degrees or "15 degrees West of North" or similar.

Maybe an option for "Mathematics" vs "Navigation" or something like that would clear some confusion.

Not sure if it adds value to what you've set out to accomplish, but no discussion of vectors would be complete without at least some sort of dot product and cross product coverage.

Well... I'm definitely not going to let you steer the plane anytime soon. :o

Math vs. Nav... Yeah, might need to make that a property that can be toggled huh?

I did come across calculations for both the dot and cross product for vectors, just couldn't find any practical examples of when those might be applied. You got any suggestions?

I believe, and please correct me if I'm wrong, both of those calculations (X and Dot) CAN be performed at design time given the properties and methods for the Vector and Line Classes that I've setup.

Granted, a similar argument could be made for the intersection of lines as well... and I've got a method setup for that. A dot and cross method would be easy enough to create if you think it's worthwhile.
 
Upvote 0
Aaron,

Very neat. Thanks. I'm coaching my son's rec soccer team. Which of course provided me with the perfect justification for putting together a subroutine to draw a soccer field! :-D

Code:
Option Explicit

Sub DrawSoccerField()
'¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯¯
'// Code to draw Soccerfield done by Greg Truby, March 2007
'// This subroutine requires Aaron Blood's class modules for
'// clsVector, clsArc and clsShape as well as his
'// Kill_Shapes routine.

    Const x As Double = 10
    Const y As Double = 20
    Const c_dblLen As Double = 115
    Const c_dblWid As Double = 75
    Const c_dScalar As Double = 6.25
    
    Dim vctGoalLineTop As clsVector, _
        vctGoalLineBott As clsVector, _
        vctSideLineRight As clsVector, _
        vctSideLineLeft As clsVector, _
        vctLineMidField As clsVector

    Dim shpCircMidField As clsShape, _
        shpDotMidField As clsShape
    
    Dim vctPos As New clsVector, _
        vctPos2 As New clsVector, _
        vctPos3 As New clsVector
    
    Dim vctGoalBoxTopLeft As New clsVector, _
        vctGoalBoxTopRight As New clsVector, _
        vctGoalBoxTopCross As New clsVector, _
        vctGoalBoxBottLeft As New clsVector, _
        vctGoalBoxBottRight As New clsVector, _
        vctGoalBoxBottCross As New clsVector
    
    Dim vctPenaltyTopLeft As New clsVector, _
        vctPenaltyTopRight As New clsVector, _
        vctPenaltyTopCross As New clsVector, _
        vctPenaltyBottLeft As New clsVector, _
        vctPenaltyBottRight As New clsVector, _
        vctPenaltyBottCross As New clsVector
        
    Dim vctGoalTop As New clsVector, _
        vctGoalBott As New clsVector, _
        vctPKLineTop As New clsVector, _
        vctPKLineBott As New clsVector, _
        arcTopPenBox As New clsArc, _
        arcBottPenBox As New clsArc
    
    Dim arcCorner1 As New clsArc, _
        arcCorner2 As New clsArc, _
        arcCorner3 As New clsArc, _
        arcCorner4 As New clsArc
        
    Dim vctHash1 As New clsVector, _
        vctHash2 As New clsVector, _
        vctHash3 As New clsVector, _
        vctHash4 As New clsVector
    
    Dim fn As WorksheetFunction
        
    Call Kill_Shapes
    
    Set vctGoalLineTop = New clsVector
    Set vctGoalLineBott = New clsVector
    Set vctSideLineLeft = New clsVector
    Set vctSideLineRight = New clsVector
    Set vctLineMidField = New clsVector
    Set shpCircMidField = New clsShape
    Set shpDotMidField = New clsShape
    
    Set fn = WorksheetFunction
        
    '// Field
    With vctGoalLineTop
        .x1 = x
        .y1 = y
        .Scalar = c_dScalar
        .Length = c_dblWid
        .Degrees = 90
        .ArrowHeadEnd = msoArrowheadNone
        .ArrowHeadBeg = msoArrowheadNone
        .LineWeight = 1.5
        .ShapeName = "<GoalLineTop"
    End With
    
    With vctSideLineRight
        .x1 = vctGoalLineTop.x2
        .y1 = vctGoalLineTop.y2
        .Scalar = c_dScalar
        .Length = c_dblLen
        .Degrees = 180
        .ArrowHeadEnd = msoArrowheadNone
        .LineWeight = 1.5
        .ShapeName = "<SideLineRight"
    End With

    With vctSideLineLeft
        .x1 = vctGoalLineTop.x1
        .y1 = vctGoalLineTop.y1
        .Scalar = c_dScalar
        .Length = c_dblLen
        .Degrees = 180
        .ArrowHeadEnd = msoArrowheadNone
        .LineWeight = 1.5
        .ShapeName = "<SideLineLeft"
    End With

    With vctGoalLineBott
        .x1 = vctSideLineLeft.x2
        .y1 = vctSideLineRight.y2
        .Scalar = c_dScalar
        .Length = c_dblWid
        .Degrees = 90
        .ArrowHeadEnd = msoArrowheadNone
        .ArrowHeadBeg = msoArrowheadNone
        .LineWeight = 1.5
        .ShapeName = "<GoalLineBott"
    End With

    '// middle of the field
    With vctLineMidField
        .x1 = vctSideLineLeft.x1
        .y1 = fn.Average(vctSideLineLeft.y1, vctSideLineLeft.y2)
        .Scalar = c_dScalar
        .Length = c_dblWid
        .Degrees = 90
        .ArrowHeadBeg = msoArrowheadNone
        .ArrowHeadEnd = msoArrowheadNone
        .LineWeight = 1
        .ShapeName = "<MidFieldLine"
    End With

    With shpCircMidField
        .y1 = vctLineMidField.y1
        .x1 = fn.Average(vctLineMidField.x1, vctLineMidField.x2)
        .Scalar = c_dScalar
        .Radius = 10
        .ShapeType = office.msoShapeOval
        .FillColor = Color.White
        .zOrder = MsoZOrderCmd.msoSendToBack
        .LineWeight = 1
        .ShapeName = "<MidFieldCircle"
    End With
    
    With shpDotMidField
        .x1 = shpCircMidField.x1
        .y1 = shpCircMidField.y1
        .Scalar = c_dScalar
        .Radius = 0.3
        .ShapeType = office.msoShapeOval
        .FillColor = Color.Black
        .zOrder = MsoZOrderCmd.msoBringForward
        .ShapeName = "<MidFieldDot"
    End With
        

    '// top goal box
    With vctPos
        .x1 = x
        .y1 = y
        .Scalar = c_dScalar
        .Length = c_dblWid / 2 - 10
        .Degrees = 90
    End With

    With vctGoalBoxTopLeft
        .x1 = vctPos.x2
        .y1 = y
        .Scalar = c_dScalar
        .Length = 6
        .Degrees = 180
        .ArrowHeadBeg = msoArrowheadNone
        .ArrowHeadEnd = msoArrowheadNone
        .LineWeight = 0.8
        .ShapeName = "<GoalTopLeft"
    End With

    With vctGoalBoxTopCross
        .x1 = vctGoalBoxTopLeft.x1
        .y1 = vctGoalBoxTopLeft.y2
        .Scalar = c_dScalar
        .Length = 20
        .Degrees = 90
        .ArrowHeadBeg = msoArrowheadNone
        .ArrowHeadEnd = msoArrowheadNone
        .LineWeight = 0.8
        .ShapeName = "<GoalTopCross"
    End With

    With vctGoalBoxTopRight
        .x1 = vctGoalBoxTopCross.x2
        .y1 = y
        .Scalar = c_dScalar
        .Length = 6
        .Degrees = 180
        .ArrowHeadBeg = msoArrowheadNone
        .ArrowHeadEnd = msoArrowheadNone
        .LineWeight = 0.8
        .ShapeName = "<GoalTopRight"
    End With

    '// Bottom Goal Box
    With vctGoalBoxBottLeft
        .x1 = vctGoalBoxTopLeft.x1
        .y1 = vctGoalLineBott.y1
        .Scalar = c_dScalar
        .Length = 6
        .Degrees = 0
        .ArrowHeadBeg = msoArrowheadNone
        .ArrowHeadEnd = msoArrowheadNone
        .LineWeight = 0.8
        .ShapeName = "<GoalBottLeft"
    End With

    With vctGoalBoxBottCross
        .x1 = vctGoalBoxTopLeft.x1
        .y1 = vctGoalBoxBottLeft.y2
        .Scalar = c_dScalar
        .Length = 20
        .Degrees = 90
        .ArrowHeadBeg = msoArrowheadNone
        .ArrowHeadEnd = msoArrowheadNone
        .LineWeight = 0.8
        .ShapeName = "<GoalBottCross"
    End With

    With vctGoalBoxBottRight
        .x1 = vctGoalBoxBottCross.x2
        .y1 = vctGoalBoxBottLeft.y1
        .Scalar = c_dScalar
        .Length = 6
        .Degrees = 0
        .ArrowHeadBeg = msoArrowheadNone
        .ArrowHeadEnd = msoArrowheadNone
        .LineWeight = 0.8
        .ShapeName = "<GoalBottRight"
    End With

    '// top penalty box
    With vctPos
        .x1 = x
        .y1 = y
        .Scalar = c_dScalar
        .Length = c_dblWid / 2 - 22
        .Degrees = 90
    End With

    With vctPenaltyTopLeft
        .x1 = vctPos.x2
        .y1 = y
        .Scalar = c_dScalar
        .Length = 18
        .Degrees = 180
        .ArrowHeadBeg = msoArrowheadNone
        .ArrowHeadEnd = msoArrowheadNone
        .LineWeight = 1
        .ShapeName = "<PenaltyTopLeft"
    End With

    With vctPenaltyTopCross
        .x1 = vctPos.x2
        .y1 = vctPenaltyTopLeft.y2
        .Scalar = c_dScalar
        .Length = 44
        .Degrees = 90
        .ArrowHeadBeg = msoArrowheadNone
        .ArrowHeadEnd = msoArrowheadNone
        .LineWeight = 1
        .ShapeName = "<PenaltyTopCross"
    End With

   With vctPenaltyTopRight
        .x1 = vctPenaltyTopCross.x2
        .y1 = y
        .Scalar = c_dScalar
        .Length = 18
        .Degrees = 180
        .ArrowHeadBeg = msoArrowheadNone
        .ArrowHeadEnd = msoArrowheadNone
        .LineWeight = 1
        .ShapeName = "<PenaltyTopRight"
    End With

    '// Bottom Penalty Box
    With vctPenaltyBottLeft
        .x1 = vctPos.x2
        .y1 = vctGoalLineBott.y1
        .Scalar = c_dScalar
        .Length = 18
        .Degrees = 0
        .ArrowHeadBeg = msoArrowheadNone
        .ArrowHeadEnd = msoArrowheadNone
        .LineWeight = 1
        .ShapeName = "<PenaltyBottLeft"
    End With
    With vctPenaltyBottCross
        .x1 = vctPos.x2
        .y1 = vctPenaltyBottLeft.y2
        .Scalar = c_dScalar
        .Length = 44
        .Degrees = 90
        .ArrowHeadBeg = msoArrowheadNone
        .ArrowHeadEnd = msoArrowheadNone
        .LineWeight = 1
        .ShapeName = "<PenaltyBottCross"
    End With
    With vctPenaltyBottRight
        .x1 = vctPenaltyBottCross.x2
        .y1 = vctPenaltyBottCross.y2
        .Scalar = c_dScalar
        .Length = 18
        .Degrees = 180
        .ArrowHeadBeg = msoArrowheadNone
        .ArrowHeadEnd = msoArrowheadNone
        .LineWeight = 1
        .ShapeName = "<PenaltyBottRight"
    End With

    '/ Top PK Line & Arc
    With vctPos
        .x1 = x
        .y1 = y
        .Length = c_dblWid / 2 - 0.5
        .Degrees = 90
    End With
    With vctPos2
        .x1 = vctPos.x2
        .y1 = y
        .Scalar = c_dScalar
        .Length = 12
        .Degrees = 180
    End With

    With vctPKLineTop
        .x1 = vctPos2.x2
        .y1 = vctPos2.y2
        .Scalar = c_dScalar
        .Length = 1
        .ArrowHeadBeg = msoArrowheadNone
        .ArrowHeadEnd = msoArrowheadNone
        .Degrees = 90
    End With

    With arcTopPenBox
        .x1 = fn.Average(vctPKLineTop.x1, vctPKLineTop.x2)
        .y1 = vctPKLineTop.y1
        .Radius = 10
        .Scalar = c_dScalar
        .Deg1 = 127
        .Deg2 = 233
        .Weight = 0.01
        .Transparency = 0
        .zOrder = MsoZOrderCmd.msoSendToBack
        .ColorIndex = Color.Black
    End With

    With vctPos
        .x1 = x
        .y1 = y
        .Length = c_dblWid / 2 - 4
        .Degrees = 90
    End With

    With vctPos2
        .x1 = vctPos.x2
        .y1 = y
        .Length = 0.5
        .Degrees = 0
    End With

    With vctGoalTop
        .x1 = vctPos.x2
        .y1 = vctPos2.y2
        .Scalar = c_dScalar
        .Length = 8
        .Degrees = 90
        .LineWeight = 2
        .ArrowHeadBeg = msoArrowheadNone
        .ArrowHeadEnd = msoArrowheadNone
    End With

    '/ Bott PK Line & Arc
    With vctPos
        .x1 = x
        .y1 = vctGoalLineBott.y1
        .Length = c_dblWid / 2 - 0.5
        .Degrees = 90
    End With
    With vctPos2
        .x1 = vctPos.x2
        .y1 = vctPos.y2
        .Scalar = c_dScalar
        .Length = 12
        .Degrees = 0
    End With

    With vctPKLineBott
        .x1 = vctPos2.x2
        .y1 = vctPos2.y2
        .Scalar = c_dScalar
        .Length = 1
        .ArrowHeadBeg = msoArrowheadNone
        .ArrowHeadEnd = msoArrowheadNone
        .Degrees = 90
    End With

    With arcBottPenBox
        .x1 = fn.Average(vctPKLineBott.x1, vctPKLineBott.x2)
        .y1 = vctPKLineBott.y1
        .Radius = 10
        .Scalar = c_dScalar
        .Deg1 = 307
        .Deg2 = 53
        .Weight = 0.01
        .Transparency = 0
        .zOrder = MsoZOrderCmd.msoSendToBack
        .ColorIndex = Color.Black
    End With

    With vctPos
        .x1 = x
        .y1 = vctGoalLineBott.y1
        .Length = c_dblWid / 2 - 4
        .Degrees = 90
    End With

    With vctPos2
        .x1 = vctPos.x2
        .y1 = vctPos.y1
        .Length = 0.5
        .Degrees = 180
    End With

    With vctGoalBott
        .x1 = vctPos.x2
        .y1 = vctPos2.y2
        .Scalar = c_dScalar
        .Length = 8
        .Degrees = 90
        .LineWeight = 2
        .ArrowHeadBeg = msoArrowheadNone
        .ArrowHeadEnd = msoArrowheadNone
    End With

    '// Corner Arcs
    With arcCorner1
        .x1 = x
        .y1 = y
        .Radius = 1.25
        .Scalar = c_dScalar
        .Deg1 = 90
        .Deg2 = 180
        .Weight = 0.08
        .Transparency = 0
        .zOrder = MsoZOrderCmd.msoSendToBack
        .ColorIndex = Color.Black
    End With
    With arcCorner2
        .x1 = vctGoalLineTop.x2
        .y1 = y
        .Radius = 1.25
        .Scalar = c_dScalar
        .Deg1 = 180
        .Deg2 = 270
        .Weight = 0.08
        .Transparency = 0
        .zOrder = MsoZOrderCmd.msoSendToBack
        .ColorIndex = Color.Black
    End With
    With arcCorner3
        .x1 = x
        .y1 = vctSideLineLeft.y2
        .Radius = 1.25
        .Scalar = c_dScalar
        .Deg1 = 0
        .Deg2 = 90
        .Weight = 0.08
        .Transparency = 0
        .zOrder = MsoZOrderCmd.msoSendToBack
        .ColorIndex = Color.Black
    End With
    With arcCorner4
        .x1 = vctGoalLineBott.x2
        .y1 = vctSideLineLeft.y2
        .Radius = 1.25
        .Scalar = c_dScalar
        .Deg1 = 270
        .Deg2 = 0
        .Weight = 0.08
        .Transparency = 0
        .zOrder = MsoZOrderCmd.msoSendToBack
        .ColorIndex = Color.Black
    End With

    '// Hash Marks
    With vctPos
        .x1 = x
        .y1 = y
        .Length = 11
        .Degrees = 90
    End With

    With vctHash1
        .x1 = vctPos.x2
        .y1 = y
        .Scalar = c_dScalar
        .Length = 1
        .Degrees = 0
        .ArrowHeadBeg = msoArrowheadNone
        .ArrowHeadEnd = msoArrowheadNone
        .LineWeight = 2
    End With

    With vctPos2
        .x1 = vctGoalLineTop.x2
        .y1 = y
        .Length = 11
        .Degrees = 270
    End With

    With vctHash2
        .x1 = vctPos2.x2
        .y1 = y
        .Scalar = c_dScalar
        .Length = 1
        .Degrees = 0
        .ArrowHeadBeg = msoArrowheadNone
        .ArrowHeadEnd = msoArrowheadNone
        .LineWeight = 2
    End With
        
    With vctHash3
        .x1 = vctHash1.x1
        .y1 = vctSideLineLeft.y2
        .Scalar = c_dScalar
        .Length = 1
        .Degrees = 180
        .ArrowHeadBeg = msoArrowheadNone
        .ArrowHeadEnd = msoArrowheadNone
        .LineWeight = 2
    End With
        
    With vctHash4
        .x1 = vctHash2.x1
        .y1 = vctSideLineLeft.y2
        .Scalar = c_dScalar
        .Length = 1
        .Degrees = 180
        .ArrowHeadBeg = msoArrowheadNone
        .ArrowHeadEnd = msoArrowheadNone
        .LineWeight = 2
    End With
        
    
    '// Draw Sequence
    vctGoalLineTop.Draw
    vctSideLineRight.Draw
    vctSideLineLeft.Draw
    vctGoalLineBott.Draw

    vctLineMidField.Draw
    shpCircMidField.Draw
    shpDotMidField.Draw

    vctGoalBoxTopLeft.Draw
    vctGoalBoxTopCross.Draw
    vctGoalBoxTopRight.Draw
    vctGoalBoxBottLeft.Draw
    vctGoalBoxBottCross.Draw
    vctGoalBoxBottRight.Draw

    vctPenaltyTopLeft.Draw
    vctPenaltyTopCross.Draw
    vctPenaltyTopRight.Draw
    vctPenaltyBottLeft.Draw
    vctPenaltyBottCross.Draw
    vctPenaltyBottRight.Draw

    vctPKLineTop.Draw
    arcTopPenBox.Draw
    vctGoalTop.Draw
    vctPKLineBott.Draw
    arcBottPenBox.Draw
    vctGoalBott.Draw

    arcCorner1.Draw
    arcCorner2.Draw
    arcCorner3.Draw
    arcCorner4.Draw

    vctHash1.Draw
    vctHash2.Draw
    vctHash3.Draw
    vctHash4.Draw
End Sub
 
Upvote 0
Cool soccer field...
~Aaron

Thanks for the compliment. But more'n that thanks for those nifty tools. :-D

Mind if I add it to the example workbook?
~Aaron

Aaron, that has to be the silliest question I've seen posted here in 2007. I'd be honored!!! :bow:
 
Upvote 0

Forum statistics

Threads
1,225,327
Messages
6,184,303
Members
453,227
Latest member
Slainte

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top