Results 1 to 4 of 4

Thread: [RESOLVED] convert pixels² to cm²

  1. #1

    Thread Starter
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,416

    Resolved [RESOLVED] convert pixels² to cm²

    i've got a formula for calculating the area of a triangle:

    vb Code:
    1. x1 = points(1).X
    2. y1 = points(1).Y
    3. x2 = points(2).X
    4. y2 = points(2).Y
    5. x3 = points(0).X
    6. y3 = points(0).Y
    7. Dim area As Decimal = (0.5 * ((x1 * (y2 - y3)) + (x2 * (y3 - y1)) + (x3 * (y1 - y2))))
    8. Debug.Print(unitConverter.pixelsSquaredToCMSquared(gr, area))

    it works well but it calculates the output in pixels² + i need it to be cm²

    heres my converter function that works well converting a pixel length to a cm length, but doesn't work with pixels²:

    vb Code:
    1. Public Shared Function pixelsSquaredToCMSquared(ByVal gr As Graphics, ByVal pixels As Decimal) As Decimal
    2.     Return (pixels / gr.DpiX) * 2.54
    3. End Function

    what am i doing wrong?

  2. #2
    Only Slightly Obsessive jemidiah's Avatar
    Join Date
    Apr 2002
    Posts
    2,431

    Re: convert pixels² to cm²

    If 100 pixels are 5 cm's [for instance], then 100*100 pixels^2 are 5*5 cm^2. That is, the conversion factor needs to get squared too.

    Example:
    Say there's 20 pixels to a cm. Convert 800 pixels^2 to some number of cm^2. Ok... since 800 = 20*40, we see

    800 pixels^2 = 20 pixels * 40 pixels = 1 cm * 2 cm = 2 cm^2.

    In general, if we have X pixels to 1 cm, and we want to convert A pixels^2 to cm^2, we use...

    A pixels^2 = (A / (X^2)) cm^2

    The formula you seem to be using is A pixels^2 = (A / X) cm^2, which will give you confusingly wrong results.
    The time you enjoy wasting is not wasted time.
    Bertrand Russell

    <- Remember to rate posts you find helpful.

  3. #3
    Only Slightly Obsessive jemidiah's Avatar
    Join Date
    Apr 2002
    Posts
    2,431

    Re: convert pixels² to cm²

    To be more explicit, the following should do what you want:

    vb Code:
    1. Public Shared Function pixelsSquaredToCMSquared(ByVal gr As Graphics, ByVal pixels As Decimal) As Decimal
    2.     Return pixels * (2.54 / gr.DpiX) ^ 2
    3. End Function
    The time you enjoy wasting is not wasted time.
    Bertrand Russell

    <- Remember to rate posts you find helpful.

  4. #4

    Thread Starter
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    26,416

    Re: convert pixels² to cm²

    thanks jemediah, that worked.
    saved a lot of head scratching rated+

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