Results 1 to 8 of 8

Thread: Convert POINTAPI type

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Oct 2008
    Posts
    126

    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

  2. #2
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    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.
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Oct 2008
    Posts
    126

    Re: Convert POINTAPI type

    Hi LaVolpe

    I need yo use Polygon and CreatePolygonRgn Gdi Api.

  4. #4
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: Convert POINTAPI type

    Quote Originally Posted by Nanni View Post
    Hi LaVolpe

    I need yo use Polygon and CreatePolygonRgn Gdi Api.
    Ok, simple question then: Why can't you just use POINTAPI only?
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Oct 2008
    Posts
    126

    Re: Convert POINTAPI type

    Hi LaVolpe

    I'm doing geometric transformations (using transformation matrix)
    and all formulas use single values

  6. #6
    VB-aholic & Lovin' It LaVolpe's Avatar
    Join Date
    Oct 2007
    Location
    Beside Waldo
    Posts
    19,541

    Re: Convert POINTAPI type

    Quote Originally Posted by Nanni View Post
    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.
    Insomnia is just a byproduct of, "It can't be done"

    Classics Enthusiast? Here's my 1969 Mustang Mach I Fastback. Her sister '67 Coupe has been adopted

    Newbie? Novice? Bored? Spend a few minutes browsing the FAQ section of the forum.
    Read the HitchHiker's Guide to Getting Help on the Forums.
    Here is the list of TAGs you can use to format your posts
    Here are VB6 Help Files online


    {Alpha Image Control} {Memory Leak FAQ} {Unicode Open/Save Dialog} {Resource Image Viewer/Extractor}
    {VB and DPI Tutorial} {Manifest Creator} {UserControl Button Template} {stdPicture Render Usage}

  7. #7
    PowerPoster RhinoBull's Avatar
    Join Date
    Mar 2004
    Location
    New Amsterdam
    Posts
    24,132

    Re: Convert POINTAPI type

    Quote Originally Posted by LaVolpe View Post
    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).

  8. #8
    VB6, XHTML & CSS hobbyist Merri's Avatar
    Join Date
    Oct 2002
    Location
    Finland
    Posts
    6,654

    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
  •  



Click Here to Expand Forum to Full Width