Results 1 to 11 of 11

Thread: [RESOLVED] cCairoMatrix - What Exactly is the Matrix? (not movie reference)

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Nov 2010
    Posts
    1,245

    Resolved [RESOLVED] cCairoMatrix - What Exactly is the Matrix? (not movie reference)

    I did a search in this forum for cCairoMatrix before posting this thread as I did try to find some previous information and did not.

    In another thread Olaf posted some excellent code that I'm deciphering line-by-line.

    https://www.vbforums.com/showthread....=1#post5553205

    There is this subroutine...

    Code:
    Friend Sub TransformDataToPixelCoords(Data() As tPoint, TDat() As tPoint)
      TDat = Data 'make a copy of the Data-Arr first...
      Dim i As Long
      For i = 0 To UBound(TDat)
          M.CalculatePointSingle TDat(i).x, TDat(i).y '...to transform x/y from data- to pixel-coords (using M)
      Next
    End Sub
    ... that takes a Data() of random x/y data and transforms it into Pixel data based on some 'matrix'. It definitely does not look like a Twips to Pixel conversion as the relationship of the values don't make sense to me.

    For example, it transforms Data(0).x (44570.5) to TDat(0).x (64) and Data(0).y (1035.846) to TDat(0).y (460.0844).

    This is not a simple division of 15 or other value that I can tell.

    Can someone dumb-down this 'matrix' thing for me and possibly tell me why it is used?

    TIA

  2. #2
    PowerPoster
    Join Date
    Jun 2013
    Posts
    7,219

    Re: cCairoMatrix - What Exactly is the Matrix? (not movie reference)

    First of all, Matrix-based calculations are common in any 3D or 2D graphics-lib...
    (even the good old GDI-API has support for them).

    If you are looking for cairo-specific infos about them here are:
    - a description of the underlying struct (and the Base-Math): https://cairographics.org/manual/cai...cairo-matrix-t
    - and the (2D)-transformations, which are supported: https://cairographics.org/manual/cai...ormations.html

    E.g. in the example you are referring to, they make sense because:
    - the x/y User-Data you come along with doesn't come "in pixels"
    - your User-Datas xMin, yMin is usually not conveniently at [0,0]
    - the scaling for x-Data and y-Data differs (obviously, because they have different "units")
    - furthermore, device-outputs usually have a Top-Down Coord-Sys
    - whereas x/y Charting wants a Bootom-Up Coord-Sys (like in school)
    - now add "Pixel-Offset-Margins" into the Mix (from the Top, Left, Right, Bottom) of your ChartCanvas-Area
    - or god-forbid "a rotation"... (which we didn't use in the example)
    ... and your "homegrown math-formulas" would become complex very fast

    A Matrix can stack-up *multiple* of these Translations, Scalings, Rotations in one single "Struct" (or as we have it in RC6, a ClassInstance).
    And the nice thing about Matrices is, that you can apply the above listed things to a Matrix "Step-by-Step".
    (in simple to comprehend instructions):

    E.g. in Picture_Resize (always, when the "outer Area-Dimensions of the Canvas wil change"),
    we build up the Matrix Step-by-Step this way:
    Code:
      'just some pre-calculations, a few lines later applied in the M.ScaleCoords-call
      xScale = (CC.Surface.Width - OffsL - OffsR) / (xMax - xMin)
      yScale = (CC.Surface.Height - OffsT - OffsB) / (yMax - yMin)
      
      Set M = Cairo.CreateIdentityMatrix 'construct a new  1:1 Matrix (in a new ClassInstance)
          M.TranslateCoords OffsL, CC.Surface.Height - OffsB
          M.ScaleCoords IIf(xScale, xScale, 0.000001), -IIf(yScale, yScale, 0.000001)
          M.TranslateCoords -xMin, -yMin
      Set MInv = M.Clone.Invert 'make an inverted Copy, for backtransformation of MouseCoords
    If the first two lines (xScale, yScale-precalc) in the above code-block are not self-explaining enough, then please just ask...

    And as for the "3 colored Steps of Matrix-Build-Up" (their Order is important):
    1) the first (blue one) makes [OffsL, CanvasHeight-OffsB] the new [0,0] point of the CoordSys
    2) the second (red one) will ensure the differing x/y-scales from this new [0,0]-point
    .. (note the blue Minus before the IIF in the second param, it ensures Bottom-Up-direction for the "y-Data")
    3) and the third one will make [-xMin, -yMin] the new [0,0] of our new "margin-offsetted and scaled" Coord-Sys

    The last line above where the MInv is derived, is just "the cherry on top",
    because it saves you the "handwritten inverse-math-routines" for back-translation of MouseCoords in one line.

    HTH for the moment...

    Olaf
    Last edited by Schmidt; Jan 23rd, 2022 at 11:58 PM.

  3. #3

    Thread Starter
    Frenzied Member
    Join Date
    Nov 2010
    Posts
    1,245

    Re: cCairoMatrix - What Exactly is the Matrix? (not movie reference)

    Quote Originally Posted by Schmidt View Post
    First of all, Matrix-based calculations are common in any 3D or 2D graphics-lib...
    (even the good old GDI-API has support for them).

    If you are looking for cairo-specific infos about them here are:
    - a description of the underlying struct (and the Base-Math): https://cairographics.org/manual/cai...cairo-matrix-t
    - and the (2D)-transformations, which are supported: https://cairographics.org/manual/cai...ormations.html


    Code:
      'just some pre-calculations, a few lines later applied in the M.ScaleCoords-call
      xScale = (CC.Surface.Width - OffsL - OffsR) / (xMax - xMin)
      yScale = (CC.Surface.Height - OffsT - OffsB) / (yMax - yMin)
      
      Set M = Cairo.CreateIdentityMatrix 'construct a new  1:1 Matrix (in a new ClassInstance)
          M.TranslateCoords OffsL, CC.Surface.Height - OffsB
          M.ScaleCoords IIf(xScale, xScale, 0.000001), -IIf(yScale, yScale, 0.000001)
          M.TranslateCoords -xMin, -yMin
      Set MInv = M.Clone.Invert 'make an inverted Copy, for backtransformation of MouseCoords
    If the first two lines (xScale, yScale-precalc) in the above code-block are not self-explaining enough, then please just ask...

    Olaf
    I will read the content from those two links. I'm starting to think my 'simple' modification to my old app is not so simple.

    As to the xScale/yScale lines, they look somewhat familiar. My math is rusty now that my hair turned gray.

    If I'm not mistaken, xScale is the value increment for each X move from left to right starting from the left offset and ends at the right offset. My guess the offsets is the space from the left and right sides (like margins) where the incremental values are contained. So if X represents the plotted values of January to December, January would start OffsL from the left and end OffsR from the right and xScale would represent the actual spacing from one plotted value to the next.

    And of course yScale would be the same vertically, perhaps representing some numerical range or whatever.


    Because of some time restraints I'd like to cut through as much red tape as possible to get to the final end result. Therefore, I have to ask/wonder whether for my purposes that I would need to deal with a MATRIX in order to accomplish my objective.

    You see, the Picturebox of my application (pctChart) is currently set to Scalemode = vbPixels (3).

    The coordinates that my candlestick price bars, moving average lines, and other drawing thingies are all currently working off the assumption that (0,0) is the top-left corner. Without having read the links provided by you *yet* of this response, I gather from your generous response that the MATRIX deals with a coordinate system that is bottom-to-top, which would answer why I ended up with an inverted conversion in the code snippet I posted here.

    So basically, what I'm wondering/asking is that, in order to achieve my end goal to replace my existing tag-along textbox to cursor clunking old way of dragging editable text around the chart with the nice crisp way of Cairo, whether I need to bother with the Matrix conversions? I fear you might say "yes, if you wish to have true DPI self-awareness." I pray that is not so! Trying to keep all these coordinate flip-flops clear in my head looks daunting at this point.

    I have just TWO things I need to accomplish with my EXISTING app;

    1. Move text around the picturebox that contains candlestick price bars and other drawings without disturbing them (erasing, etc.) until printed, allow to do another and another if desired, and even be able to EDIT the text before it becomes a permanent fixture and saved to file for later upload and be delete-able.

    2. Fill with transparent shading between the two wavy channel lines (my MomentumKeltnerChannels posted elsewhere) where the color changes between green and red depending on a trigger value.

    Will I need to involve Matrix conversions if I have no need for bottom-to-top coordinates? Do the functions/methods/controls of Cairo require it in order work with my existing top-down Pixel system?

    Again, thank you Olaf for your generosity to help a stumbling fool. :-)

  4. #4
    PowerPoster Elroy's Avatar
    Join Date
    Jun 2014
    Location
    Near Nashville TN
    Posts
    9,853

    Re: cCairoMatrix - What Exactly is the Matrix? (not movie reference)

    webbiz, there's a whole field of math often called Matrix Algebra. It's actually often called Linear Algebra, but it's actually the manipulation (addition, subtraction, multiplication, division, rotation, solving, etcetera) of these matrixes (matrices). The word "Linear" comes in because there actually is a "Linear" relationship between most matrix transformations.

    Also, in a very real sense, a single matrix is nothing more than a two-dimensional array. It just turns out that, under specific circumstances, we can use them to do powerful things in three-dimensional (actually n-dimensional) space, where an example of three-dimensional space would be the room you're sitting in.
    Any software I post in these forums written by me is provided "AS IS" without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. To all, peace and happiness.

  5. #5

    Thread Starter
    Frenzied Member
    Join Date
    Nov 2010
    Posts
    1,245

    Re: cCairoMatrix - What Exactly is the Matrix? (not movie reference)

    Quote Originally Posted by Elroy View Post
    webbiz, there's a whole field of math often called Matrix Algebra. It's actually often called Linear Algebra, but it's actually the manipulation (addition, subtraction, multiplication, division, rotation, solving, etcetera) of these matrixes (matrices). The word "Linear" comes in because there actually is a "Linear" relationship between most matrix transformations.

    Also, in a very real sense, a single matrix is nothing more than a two-dimensional array. It just turns out that, under specific circumstances, we can use them to do powerful things in three-dimensional (actually n-dimensional) space, where an example of three-dimensional space would be the room you're sitting in.
    Elroy, last night after reading Olaf's post I went to Youtube and started watching videos on Affine Transformations which then lead to Matrix Algebra and it all fell into place. To think I spent 3 years learning Algebra in High School only to have it all go *poof* over the decades (this was back in the early 70's).

    Thank you. :-)

  6. #6
    PowerPoster Elroy's Avatar
    Join Date
    Jun 2014
    Location
    Near Nashville TN
    Posts
    9,853

    Re: cCairoMatrix - What Exactly is the Matrix? (not movie reference)

    Quote Originally Posted by webbiz View Post
    To think I spent 3 years learning Algebra in High School only to have it all go *poof* over the decades
    webbiz, I wouldn't worry about it too much. Typically, in a "College Algebra" course, they just barely touch on matrix math near the end of the class (if they have time). Linear (Matrix) Algebra is an entirely different animal. For a math major (at a university), typically you get into Linear Algebra after you take at least a couple of calculus classes and a couple of geometry classes. At its most complex, each cell_item of a matrix can be a complex number or even an entire function or even just an undefined variable. However, that's all rather beyond what VB6 can deal with. You have to get into something like MatLab (where the "Mat" is short for Matrix) before you can "enjoy" the full glory of matrices.

    Just as an FYI, my full blown experience with matrices comes from statistics (so typically staying in the domain of real numbers). It's actually quite fascinating how Linear Algebra can vastly simplify (would would normally be quite complex) concepts in statistics. Also, over the last 20 years or so, I've been quite involved in software related to motion capture (with reflective markers and their 3D locations captured in 1200hz frames). To "manipulate" these markers (create virtual markers, connect them and calculate angles and dynamic forces between them) is, by far, most easily done with the aid of Linear (Matrix) Algebra. Quaternions and transformation matrices make quick work out of what's otherwise a quite complex task. Believe it or not, it even takes the trigonometry out of the calculations (as it's sort of pre-done within transformation matrices).
    Any software I post in these forums written by me is provided "AS IS" without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. To all, peace and happiness.

  7. #7

    Thread Starter
    Frenzied Member
    Join Date
    Nov 2010
    Posts
    1,245

    Re: cCairoMatrix - What Exactly is the Matrix? (not movie reference)

    Quote Originally Posted by Elroy View Post
    webbiz, I wouldn't worry about it too much. Typically, in a "College Algebra" course, they just barely touch on matrix math near the end of the class (if they have time). Linear (Matrix) Algebra is an entirely different animal. For a math major (at a university), typically you get into Linear Algebra after you take at least a couple of calculus classes and a couple of geometry classes. At its most complex, each cell_item of a matrix can be a complex number or even an entire function or even just an undefined variable. However, that's all rather beyond what VB6 can deal with. You have to get into something like MatLab (where the "Mat" is short for Matrix) before you can "enjoy" the full glory of matrices.

    Just as an FYI, my full blown experience with matrices comes from statistics (so typically staying in the domain of real numbers). It's actually quite fascinating how Linear Algebra can vastly simplify (would would normally be quite complex) concepts in statistics. Also, over the last 20 years or so, I've been quite involved in software related to motion capture (with reflective markers and their 3D locations captured in 1200hz frames). To "manipulate" these markers (create virtual markers, connect them and calculate angles and dynamic forces between them) is, by far, most easily done with the aid of Linear (Matrix) Algebra. Quaternions and transformation matrices make quick work out of what's otherwise a quite complex task. Believe it or not, it even takes the trigonometry out of the calculations (as it's sort of pre-done within transformation matrices).
    An hour ago I pulled my Algebra for Dummies book off the shelf for later reading.

    After reading this, I put it back. ;-0

    Thanks for putting it in to perspective. HS Algebra was as far as I got before entering trade school to become a computer technician. Not much need for Algebra for that. LOL!

  8. #8
    PowerPoster
    Join Date
    Jun 2013
    Posts
    7,219

    Re: cCairoMatrix - What Exactly is the Matrix? (not movie reference)

    Quote Originally Posted by webbiz View Post
    Thanks for putting it in to perspective.
    If you know roughly now, what's going on "behind the matrix" - that's entirely enough IMO.

    Just use the (cCairo)Matrix as the HelperObject it is meant to be,
    which allows you to formulate and define "complex transforms" in an easy way -
    via stacked-up, simple statements which "breaks the problem down into steps" (Translate, Scale, Rotate)

    The advantage of having Matrices in play, will be perhaps more clear -
    when you add the blue-marked part (into the Demo you linked to in your first post),
    within Picture1_MouseMove (where the inverted Matrix is in play for back-transformations):

    CurTextLabel.Content = Format(x, "Short Date") & Format(y, " (0.0)")

    So, M transforms "UserData-values" into "Pixel-Coords" (to be used as x, y params in CC.Methods).

    Whereas MInv will be used in the opposite direction - then able to reflect (Mouse)Pixel-Coords -
    back into proper UserData-Values (e.g. as render-output in a "moving ToolTip" on MouseMove).

    Olaf

  9. #9
    PowerPoster Elroy's Avatar
    Join Date
    Jun 2014
    Location
    Near Nashville TN
    Posts
    9,853

    Re: [RESOLVED] cCairoMatrix - What Exactly is the Matrix? (not movie reference)

    One more FYI. webbiz, when reading algorithms or equations with matrices in them, the matrix (or matrices) variable(s) is almost always denoted with that super-bold font. And to flesh out a bit more terminology, when a non-matrix variable (or even just a number) is included in those equations (which it often is), they're called scalars. Don't be confused by the word "scalar" though, as it just means the value is a single number. Basically, "scalars" are what we deal with all the time (well, other than strings, objects, & UDTs).
    Any software I post in these forums written by me is provided "AS IS" without warranty of any kind, expressed or implied, and permission is hereby granted, free of charge and without restriction, to any person obtaining a copy. To all, peace and happiness.

  10. #10

    Thread Starter
    Frenzied Member
    Join Date
    Nov 2010
    Posts
    1,245

    Re: [RESOLVED] cCairoMatrix - What Exactly is the Matrix? (not movie reference)

    Quote Originally Posted by Elroy View Post
    One more FYI. webbiz, when reading algorithms or equations with matrices in them, the matrix (or matrices) variable(s) is almost always denoted with that super-bold font. And to flesh out a bit more terminology, when a non-matrix variable (or even just a number) is included in those equations (which it often is), they're called scalars. Don't be confused by the word "scalar" though, as it just means the value is a single number. Basically, "scalars" are what we deal with all the time (well, other than strings, objects, & UDTs).
    Thanks Elroy.

    You are a 'scalar' and a gentleman. :-)

  11. #11

    Thread Starter
    Frenzied Member
    Join Date
    Nov 2010
    Posts
    1,245

    Re: cCairoMatrix - What Exactly is the Matrix? (not movie reference)

    Quote Originally Posted by Schmidt View Post
    If you know roughly now, what's going on "behind the matrix" - that's entirely enough IMO.

    Just use the (cCairo)Matrix as the HelperObject it is meant to be,
    which allows you to formulate and define "complex transforms" in an easy way -
    via stacked-up, simple statements which "breaks the problem down into steps" (Translate, Scale, Rotate)

    The advantage of having Matrices in play, will be perhaps more clear -
    when you add the blue-marked part (into the Demo you linked to in your first post),
    within Picture1_MouseMove (where the inverted Matrix is in play for back-transformations):

    CurTextLabel.Content = Format(x, "Short Date") & Format(y, " (0.0)")

    So, M transforms "UserData-values" into "Pixel-Coords" (to be used as x, y params in CC.Methods).

    Whereas MInv will be used in the opposite direction - then able to reflect (Mouse)Pixel-Coords -
    back into proper UserData-Values (e.g. as render-output in a "moving ToolTip" on MouseMove).

    Olaf
    It's a lot clearer now that I've watched a few videos. Turns out I'm more a visual learner.

    Basically, if I'm going to want to 'transform' a drawing, scale it, rotate etc. then the application is best done on a matrices.

    When time to take the result of the transformation and apply it to a 'pixel' coordinate surface, use the convert code.

    ================

    As for what I am trying to do, whether on the existing picturebox (pctChart) or on some other container and surface (jury is out), I don't need to do any of these complex transforms, right?

    So I could just push this 'matrix' affine transformation linear this and that aside, right?

    In other words, while the example you provided incorporated the matrix (should have taken the red pill), the same could have been done without, right? We're talking about moving text around the surface and then plopping it down somewhere to be saved with the original image.

    Thanks

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