Finding a point on a diagonal line when i have the start point and end point of the Line - vb.net

Hi am looking for some help
I have a Diagonal line drawn on a picture box on my forum and i need to know if the user has clicked the line
I have the Start point and End Point of the Line and the mouse x,y location
So i basically need to find out if the x,y of the mouse is on the line.
can anyone help?
Thanks

Example: Line Start point (A) is (0, 0), END point (B) is (10, 5).
Slope of line is therefore:
m(slope) = (y2 - y1) / (x2 - x1)
= (5 - 0) / (10 - 0)
= 5 / 10
= 0.5
To check if your point(x,y) (C) is on the line it must have the same slope from A->C and C->B. so do the same calculation again. Say point is (4, 2)
m(AC) = (2 - 0) / (4 - 0)
= 2 / 4
= 0.5
m(CB) = (5 - 2) / (10 - 4)
= 3 / 6
= 0.5
Therefore this point would be on line AB.
If point was (20, 10)
m(AC) = (10 - 0) / (20 - 0)
= 10 / 20
= 0.5
However:
m(CB) = (5 - 10) / (10 - 20)
= -5 / -10
= -0.5
Similarly if point was (2, 2)
m(AC) = (2 - 0) / (2 - 0)
= 2 / 2
= 1
m(CB) = (5 - 2) / (10 - 2)
= 3 / 8
= 0.375
So for a point to be on a line m(AB) == m(AC) == m(CB)
You may have a bit of work arounds to perform as you may not be able to get decimal values, and your line may be more than one pixel in width, but these basic principles should see you through.

Given two points, (2,4) and (-1,-2) determine the slope intercept form of the line.
1. Determine the slope
y1-y2 4-(-2) 6
----- = ------= --- = 2 = M
x1-x2 2-(-1) 3
2. To slope intercept form using one of the original points and slope from above.
(y - y1) = m(x - x1)
(y - 4) = 2(x - 2)
y - 4 = 2x - 4
y = 2x + 0 (0 is y intercept)
y = 2x (y = 2x + 0) is in slope intercept form
3. To determine if a point lies on the line, plug and chug with the new point.
new point (1,2) does y = 2x? 2 = 2(1) = true so (1,2) is on the line.
new point (2,2) does y = 2x? 2 = 2(2) = false so (2,2) is not on the line.
In your original problem you said line, but I think you might mean line segment. If you mean the latter you will also need to verify that the new x and y are within the bounds of the given segment.
The code will look something like this
Dim pta As Point = New Point(2, 4)
Dim ptb As Point = New Point(-1, -2)
Dim M As Double
If pta.X - ptb.X <> 0 Then
M = (pta.Y - ptb.Y) / (pta.X - ptb.X)
End If
'(y - pta.y) = M(x - pta.x)
'y - pta.y = Mx - m(pta.x)
'y = Mx - M(pta.x) + pta.y
Dim yIntercept As Double = (-M * pta.X) + pta.Y
Dim ptN1 As Point = New Point(1, 2)
Dim ptN2 As Point = New Point(2, 2)
If ptN1.Y = (M * (ptN1.X)) + yIntercept Then
Stop
Else
Stop
End If
If ptN2.Y = (M * (ptN2.X)) + yIntercept Then
Stop
Else
Stop
End If

Related

Rotate camera axis by mouse - Open GL

So I have successfully managed to rotate, move, etc with keyboard keys my camera through my environment. I do this by multiplying my view matrix by rotational matrices:
ViewMatrix = ViewMatrix X Z_Rotational_Matrix(yaw_increment) X X_Rotational_Matrix(pitch_increment) X Y_Rotational_Matrix(roll_increment)
I can do so with inputs of yaw, pitch, and roll. So I am next trying to do these rotations with the mouse. I believe I have all of the heavy lifting done and should just need to supply the yaw, and pitch to my rotational matrix. Correct me if this is the wrong thought process.
I can capture the mouse world coordinate when I click and when I move so supposedly it should just be math based on initial_mouse_click_coordinates and current_mouse_click_coordinates. My thought is to project two vectors, one from the initial_mouse_click_coordinate and the other from current_mouse_click_coordinates. Both vectors are parallel to the vector created from my camera location and my camera lookat point. Then I can calculate the XY planar angle and the XZ planar angle. Once these are determined I pass these as the yaw and pitch to my rotational matrices.
Determining two angles between my two lines:
The problem I have is that these values appear to be really small so on the screen nothing really happens. Am I going about this the wrong way completely?
If this is the right method to try, am I messing up on my math somewhere?
'we have initial_mouse_world_coordinates and mouse_world_coordinates. These are the points on the x screen in world coordinates.
'Two lines must be constructed going through these points, parallel with our lookat vector.
'The two angles between these two vectors are the angles to use on our lookat vector
'X = X0 + Rx*T
'Y = Y0 + Ry*T
'Z = Z0 + Rz*T
'therefore T = (Z-Z0)/Rz
'at Z = 0: T = -Z0/Rz
Dim t As Decimal = -initial_mouse_world_coordinates.Z / cam.lookat.Z
Dim y As Decimal = initial_mouse_world_coordinates.Y + (cam.lookat.Y * t)
Dim x As Decimal = initial_mouse_world_coordinates.X + (cam.lookat.X * t)
Dim z As Decimal = 0
'new point = x,y,z => translate to new vector
Dim startline As New Vector3(initial_mouse_world_coordinates.X - x, initial_mouse_world_coordinates.Y - y, initial_mouse_world_coordinates.Z - z)
t = -mouse_world_coordinates.Z / cam.lookat.Z
y = mouse_world_coordinates.Y + (cam.lookat.Y * t)
x = mouse_world_coordinates.X + (cam.lookat.X * t)
z = 0
Dim endline As New Vector3(mouse_world_coordinates.X - x, mouse_world_coordinates.Y - y, mouse_world_coordinates.Z - z)
'now simply find the two angles between these two lines
'cos(theida) = ((Ai,Ak) ⋅ (Bi,Bk)) / (||Ai,Ak|| * ||Bi,Bk||)
Try
theida = Acos(((startline.X * endline.X) + (startline.Z * endline.Z)) / (Sqrt(startline.X ^ 2 + startline.Z ^ 2) * Sqrt(endline.X ^ 2 + endline.Z ^ 2)))
Catch
theida = 0
End Try
Try
phi = Acos(((startline.X * endline.X) + (startline.Y * endline.Y)) / (Sqrt(startline.X ^ 2 + startline.Y ^ 2) * Sqrt(endline.X ^ 2 + endline.Y ^ 2)))
Catch
phi = 0
End Try
theida = theida * (180 / PI)
phi = phi * (180 / PI)
Any help or guidance is appreciated. Again I may be going at this with the wrong idea in the first place.

Random Variable not being updated in VBA Excel

While running this code to randomize some values I find that the all the cells have the same number. I believe I'm missing a standard concept but I can't seem to wrap my head around the issue.
Randomize
LWeekDay = Int((400 - 150 + 1) * Rnd + 200)
LWeekEnd = Int((600 - 200 + 1) * Rnd + 200)
For Y = 3 To 10
For X = 2 To 8
Cells(Y, X) = LWeekDay
Next X
Next Y
You are currently only calculating LWeekDay once, and then using that value for every cell.
I assume you want to assign a new value every time through the loop:
Randomize
For Y = 3 To 10
For X = 2 To 8
LWeekDay = Int((400 - 150 + 1) * Rnd + 200)
LWeekEnd = Int((600 - 200 + 1) * Rnd + 200)
Cells(Y, X) = LWeekDay
Next X
Next Y

Returning correct value from VBA script to Excel cell

I have a method I wrote in a VBA module
Public Function calcAscentTime()
IDepth = CInt(Sheets("Fundies").Range("B47"))
T = 0
T = T + 1 ' Add 1 minute for emergency
D = Math.Round((IDepth / 10) * 10) 'Round to Ceiling of nearest 10
half_depth = Math.Round(((D / 2) / 10) * 10, 0) 'Get where our first stop is
T = T + Math.Round((((D - half_depth) / 30) / 2) * 2) ' Ascend to first stop at 30ft/min
T = T + (half_depth / 10) ' 1 minute for every stop thereafter
What this does is take a value from Cell B47 on the "Fundies" worksheet and should return a value based on the calculations. I enter =calcAscentTime() into cell B48, expecting to get a value of 8(B47's value is 100), but get a return value of 0. What am I doing wrong?
ou're function isn't returning anything. Assigning the function name to the calculated variable should return the expected result (assuming you have everything else right). Something like:
Public Function calcAscentTime()
IDepth = CInt(Sheets("Fundies").Range("B47"))
T = 0
T = T + 1 ' Add 1 minute for emergency
D = Math.Round((IDepth / 10) * 10) 'Round to Ceiling of nearest 10
half_depth = Math.Round(((D / 2) / 10) * 10, 0) 'Get where our first stop is
T = T + Math.Round((((D - half_depth) / 30) / 2) * 2) ' Ascend to first stop at 30ft/min
T = T + (half_depth / 10) ' 1 minute for every stop thereafter
calcAscentTime = T
End Function
Keep in mind, you should probably Dim your variables to the proper type to avoid any kind of confusion / miss-casting by the compiler.

textbox values won't assign to a variable in vba

I was running tests on my software today and found that some of the values it was producing weren't correct.
I decided to step through the code and noticed that the variables I had assigned to textbox values on my userform when hovered over said empty, even though when hovering over the textbox assigned to it, the value inputted by the user showed.
For Example,
n = BiTimeSteps_TextBox.Value
when hovered over
n = empty
even though
BiTimeSteps_TextBox.Value = 2
when hovered over.
So say I have a formula shortly after that says
d = n*2 ,
n when hovered over says empty and d is made 0 when it shouldn't be.
Someone told me if I switch it around to
BiTimeSteps_TextBox.Value = n
it should be recognised but it is still not.
What could possibly be causing this?
See full code below: (it aims to price options using the binomial tree pricing method)
S = BiCurrentStockPrice_TextBox.Value
X = BiStrikePrice_TextBox.Value
r = BiRisk_Free_Rate_TextBox.Value
T = BiexpTime_TextBox.Value
Sigma = BiVolatility_TextBox.Value
n = BiTimeSteps_TextBox.Value
Dim i, j, k As Integer
Dim p, V, u, d, dt As Double
dt = T / n ' This finds the value of dt
u = Exp(Sigma * Sqr(dt)) 'formula for the up factor
d = 1 - u 'formula for the down factor
'V value of option
'array having the values
Dim bin() As Double 'is a binomial arrays, it stores the value of each node, there is a loop
'work out the risk free probability
p = (1 + r - d) / (u - d)
'probability of going up
ReDim bin(n + 1) As Double
'it redims the array, and n+1 is used because it starts from zero
'------------------------------------------------------------------------------------------------------------------------------
''European Call
If BiCall_CheckBox = True Then
For i = 0 To n 'payoffs = value of option at final time
bin(i + 1) = Application.WorksheetFunction.Max(0, (u ^ (n - i)) * (d ^ i) * S - X)
'It takes the max payoff or 0
Cells(i + 20, n + 2) = bin(i + 1) 'to view payoffs on the isolated column on the right
Next i
End If
'european put
If BiPut_CheckBox = True Then
For i = 0 To n 'payoffs = value of option at final time
bin(i + 1) = Application.WorksheetFunction.Max(0, X - (S * (u * (n - i)) * (d * i)))
' European Put- It takes the max payoff or 0
Cells(i + 20, n + 2) = bin(i + 1) 'to view payoffs on the isolated column on the right
Next i
End If
For k = 1 To n 'backward column loop
For j = 1 To (n - k + 1) 'loop down the column loop
bin(j) = (p * bin(j) + (1 - p) * bin(j + 1)) / (1 + r)
Cells(j + 19, n - k + 2) = bin(j)
'' print the values along the column, view of tree
Next j
Next k
Worksheets("Binomial").Cells(17, 2) = bin(1) ' print of the value V
BiOptionPrice_TextBox = bin(1)

I'm having overflow issues in this two-variable optimization program

First off, here is what I have so far:
Option Explicit
Dim y As Variant
Dim yforx As Variant
Dim yfork As Variant
Dim ynew As Variant
Dim ymin As Variant
Dim x As Variant
Dim xmin As Variant
Dim k As Variant
Dim kmin As Variant
Dim s As Variant
Dim Z As Variant
Dim Track As Variant
Sub PracticeProgram()
'Selects the right sheet
Sheets("PracticeProgram").Select
'y = k ^ 2 * (x ^ 2 + 2 * x * k - 6) / (x + k) ^ 2
'these are the bounds we are stepping through
Track = 0
x = 1
xmin = 1
k = 1
kmin = 1
y = 100000000
yforx = 100000
yfork = 1000000000
Do
y = 100000000
For x = 0 To 1000 Step 0.1
ynew = kmin ^ 2 * (x ^ 2 + 2 * x * kmin - 6) / (x + kmin) ^ 2
'This checks the new y-value against an absurdly high y-value we know is wrong. if it is less than this y-value, we keep the x-value that corresponds with it.
If ynew < y Then
xmin = x
y = ynew
yforx = y
xmin = Application.Evaluate("=Round(" & xmin & ", 3)")
Else
End If
Next
MsgBox (yforx)
For k = 0 To 1000 Step 0.1
y = k ^ 2 * (xmin ^ 2 + 2 * xmin * k - 6) / (xmin + k) ^ 2
If ynew < y Then
kmin = k
y = ynew
yfork = y
kmin = Application.Evaluate("=Round(" & kmin & ",3)")
Else
End If
Next
MsgBox (yfork)
Loop Until (Abs(yforx - yfork) < 10)
End Sub
This program is supposed to find the values of x and k in order to minimize the value of y. This is a practice for a much more complicated program that will use this same concept. In my actual program y, k, and x will all be greater than zero no matter what, but since it was hard to think of a simple equation whose results would be in the shape of a parabola opening up, I decided to allow negative answers for this practice program.
Basically, it should bounce back and forth between the equations finding the ideal values for x and k until finally it has a minimal answer for y using ideal answers for both x and k. I'm not sure what the actual answer is, so I'm letting it stop within a range of 10. If it works, I'll make it smaller, but I don't want the program going for forever, just in case.
MY PROBLEM: I keep getting overflow errors! I'm trying to round the values for xmin and kmin to three figures after the decimal, but it doesn't seem to be helping. Am I using them wrong? Can someone help me get this program working?
You're doing a division by zero. xmin = 0, k = 0, (xmin + k) ^ 2 = 0. (I'm not sure why it isn't reporting division by zero.)
A suggestion: use the Locals pane to see the value of local variables. You can also use the Watch pane to see the value of expressions you want to monitor.