|
-
Jul 15th, 2010, 06:01 AM
#1
Thread Starter
Lively Member
Convert POINTAPI type
Hi to all
I have an array of POINTAPI :
Code:
Private Type POINTAPI
X As Long
y As Long
End Type
and an Array of POINTSNG :
Code:
PrivateType POINTSNG
X As Single
y As Single
End Type
How can I convert the array of POINTSNG in a POINTAPI array without using a loop?
Thanks for suggestions
Nanni
Last edited by Nanni; Jul 15th, 2010 at 06:06 AM.
Reason: Incomplete
-
Jul 15th, 2010, 07:46 AM
#2
Re: Convert POINTAPI type
Though a single and a long both use 4 bytes, one might think CopyMemory can do the job. But don't try it. Example: if PointSNG.X was 3 and one used CopyMemory to move it to PointAPI.X, the result would be 1077936128 not 3.
Edited: If trying to move Long to Single, then same issue, different results: If PointAPI.X was 3 and moved to PointSNG.X with CopyMemory, value would be 4.203895E-45 not 3.
You will have to loop thru the array.
Edited Again. Is this for GDI+? Many GDI+ APIs have functions for arrays of singles and also arrays of long; matter of picking the right API.
Last edited by LaVolpe; Jul 15th, 2010 at 07:51 AM.
-
Jul 15th, 2010, 08:06 AM
#3
Thread Starter
Lively Member
Re: Convert POINTAPI type
Hi LaVolpe
I need yo use Polygon and CreatePolygonRgn Gdi Api.
-
Jul 15th, 2010, 08:32 AM
#4
Re: Convert POINTAPI type
 Originally Posted by Nanni
Hi LaVolpe
I need yo use Polygon and CreatePolygonRgn Gdi Api.
Ok, simple question then: Why can't you just use POINTAPI only?
-
Jul 15th, 2010, 01:43 PM
#5
Thread Starter
Lively Member
Re: Convert POINTAPI type
Hi LaVolpe
I'm doing geometric transformations (using transformation matrix)
and all formulas use single values
-
Jul 15th, 2010, 01:50 PM
#6
Re: Convert POINTAPI type
 Originally Posted by Nanni
Hi LaVolpe
I'm doing geometric transformations (using transformation matrix)
and all formulas use single values
Ugh, got thinking cap on but don't immediately see any way to convert single to long without looping. Cannot simply move bytes between arrays for reasons in my first post. Exception: use GDI+ vs GDI but not exactly effortless.
-
Jul 15th, 2010, 01:52 PM
#7
Re: Convert POINTAPI type
 Originally Posted by LaVolpe
Though a single and a long both use 4 bytes, one might think CopyMemory can do the job. But don't try it. Example: if PointSNG.X was 3 and one used CopyMemory to move it to PointAPI.X, the result would be 1077936128 not 3...
The same will happen without api by simply assigning one array to another arrayLong = arraySng (you may need to redim destination ar first).
-
Jul 15th, 2010, 03:32 PM
#8
Re: Convert POINTAPI type
Well... this code does the trick of instantly moving the array from POINTSNG array variable to a POINTAPI array variable. No looping involved at all, but API declarations required. Advantage is that this is much faster than the CopyMemory solution and uses less memory, because there is only one array data segment at all times.
Code:
Option Explicit
Private Declare Function ArrPtr Lib "msvbvm60" Alias "VarPtr" (Arr() As Any) As Long
Private Declare Sub PutMem4 Lib "msvbvm60" (ByVal Ptr As Long, ByVal Value As Long)
Private Type POINTAPI
X As Long
Y As Long
End Type
Private Type POINTSNG
X As Single
Y As Single
End Type
Private Sub Form_Load()
Dim PTSNG() As POINTSNG, PTAPI() As POINTAPI
' initialize PTSNG
ReDim PTSNG(0 To 4)
' sample data
PTSNG(0).X = 1
PTSNG(0).Y = 0.1
PTSNG(1).X = 2
PTSNG(1).Y = 0.2
PTSNG(2).X = 3
PTSNG(2).Y = 0.3
PTSNG(3).X = 4
PTSNG(3).Y = 0.4
PTSNG(4).X = 5
PTSNG(4).Y = 0.5
' make PTAPI use the header of PTSNG
PutMem4 ArrPtr(PTAPI), Not Not PTSNG
' "Not arrayvariable" will later cause an error in the IDE unless this is done
Debug.Assert App.hInstance
' also make PTSNG use no header at all
PutMem4 ArrPtr(PTSNG), 0
' sample output
Debug.Print Hex$(PTAPI(0).X), Hex$(PTAPI(0).Y)
Debug.Print Hex$(PTAPI(1).X), Hex$(PTAPI(1).Y)
Debug.Print Hex$(PTAPI(2).X), Hex$(PTAPI(2).Y)
Debug.Print Hex$(PTAPI(3).X), Hex$(PTAPI(3).Y)
Debug.Print Hex$(PTAPI(4).X), Hex$(PTAPI(4).Y)
End Sub
However the sample output shows the problem LaVolpe and RhinoBull discuss with CopyMemory: the bits remain as they are and a Single value 1 is very much a different value when handled bitwise as Long.
Output of the above code
Code:
3F800000 3DCCCCCD
40000000 3E4CCCCD
40400000 3E99999A
40800000 3ECCCCCD
40A00000 3F000000
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|