|
What Data Type are you using for the calculation in VB?.
This looks like an issue with floating-point precision.
I don't speak Idiot - please talk slowly and clearly
I don't know what all the fuss is about with America getting it's first black president. Zimbabwe's had one for years and he's sh*t. - Percy Drake , Shrewsbury
Driven to the arms of Heineken by the wife
|
|
|
|
|
Certinly I have been used Double data type for this calculation. The code inserted in below is the same
story that I have been explained.
Private Sub btnCalc_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button6.Click<br />
<br />
Dim xint, yint, zint As Double<br />
Dim ob, oc, ratio As Double<br />
Dim havint, intree As Boolean<br />
Dim x0, x1, x2, xb As Double<br />
Dim y0, y1, y2, yb As Double<br />
Dim z0, z1, z2, zb As Double<br />
'3 Points of plane formed triangle too.<br />
x0 = 2<br />
x1 = 2<br />
x2 = 2<br />
y0 = 0<br />
y1 = 2<br />
y2 = 2<br />
z0 = 2<br />
z1 = 2<br />
z2 = 0<br />
'Points of line, other point is [0 0 0]<br />
xb = 1.5<br />
yb = 1<br />
zb = 0.5<br />
Line_PlaneIntersection(x0, x1, x2, xb, y0, y1, y2, yb, z0, z1, z2, _<br />
zb, xint, yint, zint, ob, oc, ratio, havint, intree)<br />
End Sub<br />
<br />
<br />
Public Shared Sub Line_PlaneIntersect(ByVal X0 As Double, ByVal X1 As Double, ByVal X2 As Double, _<br />
ByVal Xb As Double,ByVal Y0 As Double, ByVal Y1 As Double, _<br />
ByVal Y2 As Double,ByVal Yb As Double,ByVal Z0 As Double, _ <br />
ByVal Z1 As Double, ByVal Z2 As Double, ByVal Zb As Double, _<br />
ByRef Xint As Double, ByRef Yint As Double, ByRef Zint As _<br />
Double, ByRef OB As Double, ByRef OC As Double, ByRef Ratio As _<br />
Double,ByRef HaveIntersect As Boolean, ByRef InTriangle As _<br />
Boolean) <br />
<br />
<br />
'***This formulation is based on the assumption that the line starts from origin***<br />
Dim t, u, v As Single<br />
Dim A, B, C, D As Double<br />
Dim A1, A2, A3 As Double<br />
Dim B1, B2, B3 As Double<br />
Dim C1, C2, C3 As Double<br />
Dim D1, D2, D3, D4, D5, D6 As Double<br />
Dim Det As Double<br />
Dim Det1, Det2, Det3, Det4, Det5, Det6 As Double<br />
Det1 = +Xb * ((Y2 - Y0) * (Z1 - Z0))<br />
Det2 = +Yb * ((X1 - X0) * (Z2 - Z0))<br />
Det3 = +Zb * ((X2 - X0) * (Y1 - Y0))<br />
Det4 = -Xb * ((Y1 - Y0) * (Z2 - Z0))<br />
Det5 = -Yb * ((X2 - X0) * (Z1 - Z0))<br />
Det6 = -Zb * ((X1 - X0) * (Y2 - Y0))<br />
Det = Det1 + Det2 + Det3 + Det4 + Det5 + Det6<br />
If Det <> 0 Then<br />
HaveIntersect = True<br />
A1 = X0 * ((Y1 - Y0) * (Z2 - Z0) - (Y2 - Y0) * (Z1 - Z0))<br />
A2 = Y0 * ((X2 - X0) * (Z1 - Z0) - (X1 - X0) * (Z2 - Z0))<br />
A3 = Z0 * ((X1 - X0) * (Y2 - Y0) - (X2 - X0) * (Y1 - Y0))<br />
A = A1 + A2 + A3<br />
B1 = X0 * ((Y2 - Y0) * Zb - (Z2 - Z0) * Yb)<br />
B2 = Y0 * ((Z2 - Z0) * Xb - (X2 - X0) * Zb)<br />
B3 = Z0 * ((X2 - X0) * Yb - (Y2 - Y0) * Xb)<br />
B = B1 + B2 + B3<br />
C1 = X0 * ((Z1 - Z0) * Yb - (Y1 - Y0) * Zb)<br />
C2 = Y0 * ((X1 - X0) * Zb - (Z1 - Z0) * Xb)<br />
C3 = Z0 * ((Y1 - Y0) * Xb - (X1 - X0) * Yb)<br />
C = C1 + C2 + C3<br />
D1 = +(X1 - X0) * (Y2 - Y0) * Zb<br />
D2 = +(X2 - X0) * (Z1 - Z0) * Yb<br />
D3 = +(Y1 - Y0) * (Z2 - Z0) * Xb<br />
D4 = -(X1 - X0) * (Z2 - Z0) * Yb<br />
D5 = -(X2 - X0) * (Y1 - Y0) * Zb<br />
D6 = -(Y2 - Y0) * (Z1 - Z0) * Xb<br />
D = D1 + D2 + D3 + D4 + D5 + D6<br />
t = A / D<br />
u = B / D<br />
v = C / D<br />
Xint = Xb * t<br />
Yint = Yb * t<br />
Zint = Zb * t <br />
Dim Bool1, Bool2 As Boolean<br />
Bool1 = u >= 0 And u <= 1<br />
Bool2 = v >= 0 And v <= 1<br />
If Bool1 And Bool2 And (u + v) <= 1 Then<br />
'means that the intersection point lies in triangle wroughted from 3 points of the plane <br />
InTriangle = True<br />
OB = Math.Sqrt(Xb ^ 2 + Yb ^ 2 + Zb ^ 2)<br />
OC = Math.Sqrt(Xint ^ 2 + Yint ^ 2 + Zint ^ 2)<br />
Ratio = OB / OC<br />
End If<br />
Else<br />
HaveIntersect = False<br />
InTriangle = False<br />
End If<br />
End Sub
'***************************************************************************************************
%MAtlab syntax
clear<br />
%Data of line's points<br />
xa=0; ya=0; za=0;<br />
xb=1.5; yb=1; zb=1/2;<br />
Ia=[xa<br />
ya<br />
za];<br />
Ib=[xb<br />
yb<br />
zb];<br />
%Data of plane<br />
x0=2; y0=0; z0=2;<br />
x1=2; y1=2; z1=2;<br />
x2=2; y2=2; z2=0;<br />
A= [xa-xb x1-x0 x2-x0<br />
ya-yb y1-y0 y2-y0<br />
za-zb z1-z0 z2-z0];<br />
B=[xa-x0<br />
ya-y0<br />
za-z0];<br />
<br />
C=A^-1*B<br />
<br />
Iint=Ia+(Ib-Ia).*C(1)<br />
after running
Iint =
2.0000
1.3333
0.6667
|
|
|
|
|
I am not Maths Wizard (I was a terrible student), but this excerpt from Wikipaedia on double floating point precision may shed some light...
Why do you need to return so many digits after the decimal point?
Addition and subtraction
A simple method to add floating-point numbers is to first represent them with the same exponent. In the example below, the second number is shifted right by three digits, and we then proceed with the usual addition method:
123456.7 = 1.234567 * 10^5
101.7654 = 1.017654 * 10^2 = 0.001017654 * 10^5
Hence:
123456.7 + 101.7654 = (1.234567 * 10^5) + (1.017654 * 10^2)
= (1.234567 * 10^5) + (0.001017654 * 10^5)
= (1.234567 + 0.001017654) * 10^5
= 1.235584654 * 10^5
In detail:
e=5; s=1.234567 (123456.7)
+ e=2; s=1.017654 (101.7654)
e=5; s=1.234567
+ e=5; s=0.001017654 (after shifting)
--------------------
e=5; s=1.235584654 (true sum: 123558.4654)
This is the true result, the exact sum of the operands. It will be rounded to seven digits and then normalized if necessary. The final result is
e=5; s=1.235585 (final sum: 123558.5)
Note that the low 3 digits of the second operand (654) are essentially lost. This is round-off error. In extreme cases, the sum of two non-zero numbers may be equal to one of them:
e=5; s=1.234567
+ e=-3; s=9.876543
e=5; s=1.234567
+ e=5; s=0.00000009876543 (after shifting)
----------------------
e=5; s=1.23456709876543 (true sum)
e=5; s=1.234567 (after rounding/normalization)
Another problem of loss of significance occurs when two close numbers are subtracted. In the following example e = 5; s = 1.234571 and e = 5; s = 1.234567 are representations of the rationals 123457.1467 and 123456.659.
e=5; s=1.234571
- e=5; s=1.234567
----------------
e=5; s=0.000004
e=-1; s=4.000000 (after rounding/normalization)
The best representation of this difference is e = −1; s = 4.877000 , which differs more than 20% from e = −1; s = 4.000000 . In extreme cases, the final result may be zero even though an exact calculation may be several million. This cancellation illustrates the danger in assuming that all of the digits of a computed result are meaningful. Dealing with the consequences of these errors is a topic in numerical analysis; see also Accuracy problems.
I don't speak Idiot - please talk slowly and clearly
I don't know what all the fuss is about with America getting it's first black president. Zimbabwe's had one for years and he's sh*t. - Percy Drake , Shrewsbury
Driven to the arms of Heineken by the wife
|
|
|
|
|
I find answer! Because I had been defined three of variable as Single.
However this variables were small but when I change those to Double this problem solved.
Dim t, u, v As Single -------->Dim t, u, v As Double
Sorry
|
|
|
|
|
Using the Double datatype does not "solve" the problem. It mearly masks it. It still has the same problem as the Single type. Google and read up on "IEEE 754" and "IEEE 854" to understand how number representation works in computers and why you're getting the results you are.
|
|
|
|
|
Hmm. In this case I think single/double is the root cause; the OP complained about the numbers his code produced (which displayed some 16 digits of which obviously only 7 or so were correct). He wasn't really worried about the very last digit (yet).
Luc Pattyn
I only read code that is properly indented, and rendered in a non-proportional font; hint: use PRE tags in forum messages
Local announcement (Antwerp region): Lange Wapper? Neen!
|
|
|
|
|
i want to insert some values "from visual basic into one m-file in matlab" then run another m-file from visual basic that will get benefit from the first m-file with the inserted values. i really would apprecitae any help. this is what i wrote so far in visual basic:
Dim Matlab As Object
Dim Result As String
Dim MReal (0,3) As Double
Dim MImag() As Double
Set Matlab = createObject("Matlab.Application")
' here where i saved the two m-files
Result = Matlab.Excute("C:\Porgram Files\MATLAB71\work")
'this is the second m-file which i want to run
Result = Matlab.Excute("solvingnle")
Call Matlab.GetFullMatrix("X", "base", MReal, MImag)
'here where i will show the result of my m-file, each result of the matrix seperate
Text1.Text = MReal(0, 0)
Text2.Text = MReal(0, 1)
Text3.Text = MReal(0, 2)
Text4.Text = MReal(0, 3)
End Sub
|
|
|
|
|
I'm building a dataset by querying several different Oracle tables with the same query.
I want to output the dataset to an Excel file with a separate heading for the results from each Oracle table.
To do this, so far I've added a 'Heading" column to the datatable, but I can't figure out how to fill that column to identify the datarows that came from each Oracle table.
If anyone can shed some light on this it would be appreciated.
For i = 0 To 3
Try
CN = New OracleConnection("Data Source=xe;User Id=" & Username(i) & ";Password=" & Password(i))
'create the dataadapter object
Dim adp As New OracleDataAdapter(SQL(i), CN)
adp.Fill(ds)
If i = 0 Then
ds.Tables(0).Columns.Add("Headings")
End If
|
|
|
|
|
I don't think that you need to add a 'Headings' column.
A DataSet has a Tables collection, each DataTable in that collection has a TableName property. so that is accessible by iterating over the Tables collection.
Each DataTable has a Columns collection which is a collection of DataColumn s, each of which has a ColumnName property.
So iterate over the Tables collection and for each DataTable returned iterate over its Columns collection. For each DataColumn , get its ColumnName .
Henry Minute
Do not read medical books! You could die of a misprint. - Mark Twain
Girl: (staring) "Why do you need an icy cucumber?"
“I want to report a fraud. The government is lying to us all.”
|
|
|
|
|
Hi. Thanks for your input.
I'm iterating through a list of users of an Oracle database and running the same query on each. I create a new Oracle dataadapter for each user.
I would have thought that VB would fill a separate table with each user's query results. For some reason it's putting them all in the same table: Table(0).
It would be much easier to handle things if they did go into separate tables, but I can't see how to do this. Do you have any suggestions?
Thanks again.
|
|
|
|
|
Without seeing your code, it is difficult to be certain, and I am also not familiar with Oracle from a .Net point of view.
But if you are iterating a user list, it should be possible to create a new Table on each loop and have the query results added to that Table which then gets added to the DataSet.
Henry Minute
Do not read medical books! You could die of a misprint. - Mark Twain
Girl: (staring) "Why do you need an icy cucumber?"
“I want to report a fraud. The government is lying to us all.”
|
|
|
|
|
Creating a new table for each user in your list would be exceedingly tedious to manage and also adds a lot of weight to your data in memory. DataTables are not light-weight objects. So, if you have thousands of users, you're going to consuming vast amounts of memory, needlessly.
From your limited description, it sounds like you have a serious design flaw in your data model.
|
|
|
|
|
Hello to All,
I am creating file with .ttg extenction. And i want to open those files with my application(Default) by using programming.
So how can i do that?
Thanks for help
If you can think then I Can.
|
|
|
|
|
look here[^]
Steve Jowett
-------------------------
Real programmers don't comment their code. If it was hard to write, it should be hard to read.
|
|
|
|
|
I am a beginner in VB.
I am creating a Window Application. I want to authenticate it with username and password, on first screen.
How can it be done?
Thank You.
|
|
|
|
|
|
Andy, you're such a gentle, helpful soul, I do beleive the OP univoted you, probably because you did not snd codz. Have a balancer.
Never underestimate the power of human stupidity
RAH
|
|
|
|
|
Mycroft Holmes wrote: Have a balancer.
And another.
I have a growing suspicion that word is out in certain areas that Code Project is better and faster than Google.
|
|
|
|
|
I want to make an application that when I'm with the mouse on it ... it sets the opacity to 100 ... and when the mouse is .. outside the form.. it sets the opacity to 50
THE PROBLEM IS that the code...is working when the mouse is over the content of the form... it does not include the border of the form or the form controlbox(min,max,close) ... don't know why
I've managed to make something like that with a timer set to 100 and with this piece of code:
If Cursor.Position.X >= Me.Left And Cursor.Position.X <= Me.Right Then
If Cursor.Position.Y >= Me.Top And Cursor.Position.Y <= Me.Bottom Then
mouseover = True
Else
mouseover = False
End If
Else
mouseover = False
End If
Please help with an idea .. Thanks
|
|
|
|
|
That's because those events only fire inside the client area of the form. What you area referring to is the non-client area, borders and titlebar. There are no events for those areas.
You'd have to override the forms WndProc and capture the window messages, like WM_NCHITTEST, directly to to see if the mouse is over those areas. If so, you just fire your own custom events if you want, or make calls directly to some method, depending on what you need to do.
|
|
|
|
|
Thank you very much for your answer! I've done it and also I've just learned something new
The code for doing that is:
Protected Overrides Sub WndProc(ByRef m As Message)
MyBase.WndProc(m)
Dim WM_NCHITTEST = &H84
If m.Msg = WM_NCHITTEST Then
MsgBox("mouse is over")
End If
End Sub
Thanks!
|
|
|
|
|
I am reading from a text file and inserting a line "REF*LU*" at various stages in my text file. The ending textfile is in UNIcode/Little Endian; how do i convert this to ASCII? See my code below
Dim newString As New StringBuilder<br />
newString.Append(My.Computer.FileSystem.ReadAllText(FileName))<br />
newString.Replace(oldISAGS, newISAGS)<br />
newString.Replace(myString, myString & vbNewLine & "REF*LU*" & refNum & "~")<br />
newString.Replace(myString2, myString2 & vbNewLine & "REF*LU*" & refNum & "~")<br />
My.Computer.FileSystem.WriteAllText(FileName, newString.ToString, False)
Thanks.
|
|
|
|
|
1.
My.Computer.FileSystem.WriteAllText, without an explicit Encoding, creates a UTF-8, not a Unicode, file; the documentation says:
"When no encoding is specified, UTF-8 is used. The byte order mark (BOM) for the encoding is written to the file unless you specify Encoding.Default, which uses the system's current ANSI code page..."
If you don't want a BOM, use Encoding.Default; or use File.WriteAllText which works without BOM by default.
2.
why do you use a StringBuilder here? I don't think it will be faster than similar string operations in this app, as all you do are global operations, not a long sequence of appends.
Luc Pattyn
I only read code that is properly indented, and rendered in a non-proportional font; hint: use PRE tags in forum messages
Local announcement (Antwerp region): Lange Wapper? Neen!
|
|
|
|
|
Luc,
Thanks for your comments.
I used StringBuilder at the time on the fly when this was a simplier applet; it still works well for the intended use. But I agreed that one day I should clean it up and use more global codes instead of a series of appends.
Can you give me a sample of where in the code and how I should use the encoding.ASCII?
|
|
|
|
|
IvanIT wrote: Can you give me ...
I could, but I'm not going to.
I explained two ways to solve your problem, now use the documentation to figure out the details. Don't be lazy, and learn.
Luc Pattyn
I only read code that is properly indented, and rendered in a non-proportional font; hint: use PRE tags in forum messages
Local announcement (Antwerp region): Lange Wapper? Neen!
|
|
|
|
|