Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Algorithms
  4. WMP Visualization help

WMP Visualization help

Scheduled Pinned Locked Moved Algorithms
18 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • X Offline
    X Offline
    XTAL256
    wrote on last edited by
    #1

    Hi, i am making a Windows Media Player Visualization and i need some help with an algorithm. I am making a snake like curve that moves around the screen. It uses 3 point bezier curves joined together to form one long curve. The head continually grows while the end disappears. I need to make each new one connect to the old head but my code doesn't work. Here is some of the code:

    typedef struct tagBezier{
    float x, y; // Curve position
    POINTF P0, P1, P2; // Control points
    float t, t2; // Time variable [0-1]
    } Bezier;

    void CViz3D::calculateBezier(Bezier& b, float time) {
    b.t2 = b.t; b.t = time;

    b.x = (pow(b.P0.x\*(1 - b.t),2) + b.P1.x\*2\*b.t\*(1 - b.t) + pow(b.P2.x\*b.t,2));
    b.y = (pow(b.P0.y\*(1 - b.t),2) + b.P1.y\*2\*b.t\*(1 - b.t) + pow(b.P2.y\*b.t,2));
    

    }

    void CViz3D::drawBezierCurve(Bezier b[CURVES]) {
    float iStart = 0, iEnd = 1;

    if (b\[start\].t < b\[start\].t2) {  // Change position if finished
        int sOld = start;   // Previous start pos
        start = (start+1)%CURVES;
        end = (end+1)%CURVES;
        float distX=b\[sOld\].P2.x-b\[sOld\].P1.x, distY=b\[sOld\].P2.y-b\[sOld\].P1.y;
        b\[start\].P0.x = b\[sOld\].P2.x;       b\[start\].P0.y = b\[sOld\].P2.y;
        b\[start\].P1.x = b\[sOld\].P2.x+distX; b\[start\].P1.x = b\[sOld\].P2.y+distY;
        b\[start\].P2.x = (RAND-0.5f);        b\[start\].P2.y = (RAND-0.5f);
    }
    
    for (int j = 0; j < CURVES; j++) {
        if (j == start) {  iStart = 0;  iEnd = bezTime; }
        else if (j == end){ iStart = bezTime; iEnd = 1; }
        else { iStart = 0; iEnd = 1; }
    
        glBegin(GL\_LINE\_STRIP);
        for (float i = iStart; i < iEnd; i += 0.01f) {
            calculateBezier(b\[j\], i);
            glVertex2f(b\[j\].x, b\[j\].y);
        }
        glEnd();
    }
    

    }

    Can someone figure out what's going on. Thanx

    L U 2 Replies Last reply
    0
    • X XTAL256

      Hi, i am making a Windows Media Player Visualization and i need some help with an algorithm. I am making a snake like curve that moves around the screen. It uses 3 point bezier curves joined together to form one long curve. The head continually grows while the end disappears. I need to make each new one connect to the old head but my code doesn't work. Here is some of the code:

      typedef struct tagBezier{
      float x, y; // Curve position
      POINTF P0, P1, P2; // Control points
      float t, t2; // Time variable [0-1]
      } Bezier;

      void CViz3D::calculateBezier(Bezier& b, float time) {
      b.t2 = b.t; b.t = time;

      b.x = (pow(b.P0.x\*(1 - b.t),2) + b.P1.x\*2\*b.t\*(1 - b.t) + pow(b.P2.x\*b.t,2));
      b.y = (pow(b.P0.y\*(1 - b.t),2) + b.P1.y\*2\*b.t\*(1 - b.t) + pow(b.P2.y\*b.t,2));
      

      }

      void CViz3D::drawBezierCurve(Bezier b[CURVES]) {
      float iStart = 0, iEnd = 1;

      if (b\[start\].t < b\[start\].t2) {  // Change position if finished
          int sOld = start;   // Previous start pos
          start = (start+1)%CURVES;
          end = (end+1)%CURVES;
          float distX=b\[sOld\].P2.x-b\[sOld\].P1.x, distY=b\[sOld\].P2.y-b\[sOld\].P1.y;
          b\[start\].P0.x = b\[sOld\].P2.x;       b\[start\].P0.y = b\[sOld\].P2.y;
          b\[start\].P1.x = b\[sOld\].P2.x+distX; b\[start\].P1.x = b\[sOld\].P2.y+distY;
          b\[start\].P2.x = (RAND-0.5f);        b\[start\].P2.y = (RAND-0.5f);
      }
      
      for (int j = 0; j < CURVES; j++) {
          if (j == start) {  iStart = 0;  iEnd = bezTime; }
          else if (j == end){ iStart = bezTime; iEnd = 1; }
          else { iStart = 0; iEnd = 1; }
      
          glBegin(GL\_LINE\_STRIP);
          for (float i = iStart; i < iEnd; i += 0.01f) {
              calculateBezier(b\[j\], i);
              glVertex2f(b\[j\].x, b\[j\].y);
          }
          glEnd();
      }
      

      }

      Can someone figure out what's going on. Thanx

      L Offline
      L Offline
      Luc Pattyn
      wrote on last edited by
      #2

      Hi, may I suggest you expand on "does not work" before you expect anyone to dig in ? Does it compile ? run ? behave for a while ? then what ? :)

      Luc Pattyn [My Articles]

      X U 2 Replies Last reply
      0
      • L Luc Pattyn

        Hi, may I suggest you expand on "does not work" before you expect anyone to dig in ? Does it compile ? run ? behave for a while ? then what ? :)

        Luc Pattyn [My Articles]

        X Offline
        X Offline
        XTAL256
        wrote on last edited by
        #3

        Sorry i should have mentioned, the curves don't match up. That is, when a new curve is made it should smoothly match up with the one before it. Here is an image that explains it. I can't be bothered posting an image of the actual result but basically point B2 seems to be at any random spot instead of being in line with A2. http://img89.imageshack.us/img89/318/bezierlt5.png

        L U 2 Replies Last reply
        0
        • X XTAL256

          Sorry i should have mentioned, the curves don't match up. That is, when a new curve is made it should smoothly match up with the one before it. Here is an image that explains it. I can't be bothered posting an image of the actual result but basically point B2 seems to be at any random spot instead of being in line with A2. http://img89.imageshack.us/img89/318/bezierlt5.png

          L Offline
          L Offline
          Luc Pattyn
          wrote on last edited by
          #4

          Hi, some remarks: 1. you managed to confuse yourself and me by using different numbering in code and drawing; 2. your code makes two distances equal: A3-A2 = B2-B1 in drawing's notation, although drawing's annotation says otherwise. 3. you are aware I trust that RAND is called twice and will return two different numbers; this yields independent x and y for B3 (is OK). 4. but I would make the distance B3-B2 random, so I prefer something like:

          b[start].P2.x = b[start].P1.x+maxStepX*(RAND-0.5f);
          b[start].P2.y = b[start].P1.y+maxStepY*(RAND-0.5f);

          with maxStepX and maxStepY some reasonable constants (maybe equal). Hope this helps.

          Luc Pattyn [My Articles]

          X U 2 Replies Last reply
          0
          • L Luc Pattyn

            Hi, some remarks: 1. you managed to confuse yourself and me by using different numbering in code and drawing; 2. your code makes two distances equal: A3-A2 = B2-B1 in drawing's notation, although drawing's annotation says otherwise. 3. you are aware I trust that RAND is called twice and will return two different numbers; this yields independent x and y for B3 (is OK). 4. but I would make the distance B3-B2 random, so I prefer something like:

            b[start].P2.x = b[start].P1.x+maxStepX*(RAND-0.5f);
            b[start].P2.y = b[start].P1.y+maxStepY*(RAND-0.5f);

            with maxStepX and maxStepY some reasonable constants (maybe equal). Hope this helps.

            Luc Pattyn [My Articles]

            X Offline
            X Offline
            XTAL256
            wrote on last edited by
            #5

            "1. you managed to confuse yourself and me by using different numbering in code and drawing" yeah, sorry about that. I draw the picture before i wrote the code, to give me an idea of what to do... that didn't help :) "2. your code makes two distances equal:..." yes, i changed that to make it easier to code "4. but I would make the distance B3-B2 random..." isn't that basically the same as what i did, except i picked random points. Thanks for your help. But my problem it that B2 isn't lined up with A2 and i don't know why?

            L U 2 Replies Last reply
            0
            • X XTAL256

              "1. you managed to confuse yourself and me by using different numbering in code and drawing" yeah, sorry about that. I draw the picture before i wrote the code, to give me an idea of what to do... that didn't help :) "2. your code makes two distances equal:..." yes, i changed that to make it easier to code "4. but I would make the distance B3-B2 random..." isn't that basically the same as what i did, except i picked random points. Thanks for your help. But my problem it that B2 isn't lined up with A2 and i don't know why?

              L Offline
              L Offline
              Luc Pattyn
              wrote on last edited by
              #6

              Hi, I looked at your code again and discovered you are squaring the coordinates !?!? b.P0.x and the like should not be inside pow(...,2) ! Also I would suggest to keep the bezier curves large enough, i.e. I hope you are not trying to draw a bezier that only spans a few pixels (would waste CPU cycles and might be off due to quantization effects). :)

              Luc Pattyn [My Articles]

              X U 2 Replies Last reply
              0
              • L Luc Pattyn

                Hi, I looked at your code again and discovered you are squaring the coordinates !?!? b.P0.x and the like should not be inside pow(...,2) ! Also I would suggest to keep the bezier curves large enough, i.e. I hope you are not trying to draw a bezier that only spans a few pixels (would waste CPU cycles and might be off due to quantization effects). :)

                Luc Pattyn [My Articles]

                X Offline
                X Offline
                XTAL256
                wrote on last edited by
                #7

                Thanks for that. :) I never would have found that out. I got the formula for Bezier curves off the internet and coded it in C++ but i didn't keep the original formula so i just assumed i copied it right. Thanks again.

                L U 2 Replies Last reply
                0
                • X XTAL256

                  Thanks for that. :) I never would have found that out. I got the formula for Bezier curves off the internet and coded it in C++ but i didn't keep the original formula so i just assumed i copied it right. Thanks again.

                  L Offline
                  L Offline
                  Luc Pattyn
                  wrote on last edited by
                  #8

                  You're welcome. When in doubt about some formula, I check the edge conditions, in your case the Bezier must yield P0 and P2 for the special values t=0 and t=1. Furthermore, squaring a physical thing (such as a coordinate) does not occur often. Best regards,

                  Luc Pattyn [My Articles]

                  X U 2 Replies Last reply
                  0
                  • L Luc Pattyn

                    You're welcome. When in doubt about some formula, I check the edge conditions, in your case the Bezier must yield P0 and P2 for the special values t=0 and t=1. Furthermore, squaring a physical thing (such as a coordinate) does not occur often. Best regards,

                    Luc Pattyn [My Articles]

                    X Offline
                    X Offline
                    XTAL256
                    wrote on last edited by
                    #9

                    There is still just one little problem. Sometimes the curves don't go from one to the other and stay there. That is, one curve keeps repeating over and over as if if (b[start].t < b[start].t2) doesn't evaluate to true??? Here is a link to download the viz so you can see for yourself. http://www.savefile.com/files/550244[^] Read the readme, it tells you how to install it. If you can't get it to install tell me and i'll post some pics instead.

                    U 1 Reply Last reply
                    0
                    • L Luc Pattyn

                      You're welcome. When in doubt about some formula, I check the edge conditions, in your case the Bezier must yield P0 and P2 for the special values t=0 and t=1. Furthermore, squaring a physical thing (such as a coordinate) does not occur often. Best regards,

                      Luc Pattyn [My Articles]

                      U Offline
                      U Offline
                      User 12349108
                      wrote on last edited by
                      #10

                      thanks: https://movied.org

                      1 Reply Last reply
                      0
                      • X XTAL256

                        "1. you managed to confuse yourself and me by using different numbering in code and drawing" yeah, sorry about that. I draw the picture before i wrote the code, to give me an idea of what to do... that didn't help :) "2. your code makes two distances equal:..." yes, i changed that to make it easier to code "4. but I would make the distance B3-B2 random..." isn't that basically the same as what i did, except i picked random points. Thanks for your help. But my problem it that B2 isn't lined up with A2 and i don't know why?

                        U Offline
                        U Offline
                        User 12349108
                        wrote on last edited by
                        #11

                        thanks: https://movied.org

                        1 Reply Last reply
                        0
                        • X XTAL256

                          There is still just one little problem. Sometimes the curves don't go from one to the other and stay there. That is, one curve keeps repeating over and over as if if (b[start].t < b[start].t2) doesn't evaluate to true??? Here is a link to download the viz so you can see for yourself. http://www.savefile.com/files/550244[^] Read the readme, it tells you how to install it. If you can't get it to install tell me and i'll post some pics instead.

                          U Offline
                          U Offline
                          User 12349108
                          wrote on last edited by
                          #12

                          thanks: https://movied.org

                          1 Reply Last reply
                          0
                          • L Luc Pattyn

                            Hi, I looked at your code again and discovered you are squaring the coordinates !?!? b.P0.x and the like should not be inside pow(...,2) ! Also I would suggest to keep the bezier curves large enough, i.e. I hope you are not trying to draw a bezier that only spans a few pixels (would waste CPU cycles and might be off due to quantization effects). :)

                            Luc Pattyn [My Articles]

                            U Offline
                            U Offline
                            User 12349108
                            wrote on last edited by
                            #13

                            thanks: https://movied.org

                            1 Reply Last reply
                            0
                            • L Luc Pattyn

                              Hi, some remarks: 1. you managed to confuse yourself and me by using different numbering in code and drawing; 2. your code makes two distances equal: A3-A2 = B2-B1 in drawing's notation, although drawing's annotation says otherwise. 3. you are aware I trust that RAND is called twice and will return two different numbers; this yields independent x and y for B3 (is OK). 4. but I would make the distance B3-B2 random, so I prefer something like:

                              b[start].P2.x = b[start].P1.x+maxStepX*(RAND-0.5f);
                              b[start].P2.y = b[start].P1.y+maxStepY*(RAND-0.5f);

                              with maxStepX and maxStepY some reasonable constants (maybe equal). Hope this helps.

                              Luc Pattyn [My Articles]

                              U Offline
                              U Offline
                              User 12349108
                              wrote on last edited by
                              #14

                              thanks: https://movied.org

                              1 Reply Last reply
                              0
                              • X XTAL256

                                Thanks for that. :) I never would have found that out. I got the formula for Bezier curves off the internet and coded it in C++ but i didn't keep the original formula so i just assumed i copied it right. Thanks again.

                                U Offline
                                U Offline
                                User 12349108
                                wrote on last edited by
                                #15

                                thanks: https://movied.org

                                1 Reply Last reply
                                0
                                • L Luc Pattyn

                                  Hi, may I suggest you expand on "does not work" before you expect anyone to dig in ? Does it compile ? run ? behave for a while ? then what ? :)

                                  Luc Pattyn [My Articles]

                                  U Offline
                                  U Offline
                                  User 12349108
                                  wrote on last edited by
                                  #16

                                  thanks: https://movied.org

                                  1 Reply Last reply
                                  0
                                  • X XTAL256

                                    Sorry i should have mentioned, the curves don't match up. That is, when a new curve is made it should smoothly match up with the one before it. Here is an image that explains it. I can't be bothered posting an image of the actual result but basically point B2 seems to be at any random spot instead of being in line with A2. http://img89.imageshack.us/img89/318/bezierlt5.png

                                    U Offline
                                    U Offline
                                    User 12349108
                                    wrote on last edited by
                                    #17

                                    thanks: https://movied.org

                                    1 Reply Last reply
                                    0
                                    • X XTAL256

                                      Hi, i am making a Windows Media Player Visualization and i need some help with an algorithm. I am making a snake like curve that moves around the screen. It uses 3 point bezier curves joined together to form one long curve. The head continually grows while the end disappears. I need to make each new one connect to the old head but my code doesn't work. Here is some of the code:

                                      typedef struct tagBezier{
                                      float x, y; // Curve position
                                      POINTF P0, P1, P2; // Control points
                                      float t, t2; // Time variable [0-1]
                                      } Bezier;

                                      void CViz3D::calculateBezier(Bezier& b, float time) {
                                      b.t2 = b.t; b.t = time;

                                      b.x = (pow(b.P0.x\*(1 - b.t),2) + b.P1.x\*2\*b.t\*(1 - b.t) + pow(b.P2.x\*b.t,2));
                                      b.y = (pow(b.P0.y\*(1 - b.t),2) + b.P1.y\*2\*b.t\*(1 - b.t) + pow(b.P2.y\*b.t,2));
                                      

                                      }

                                      void CViz3D::drawBezierCurve(Bezier b[CURVES]) {
                                      float iStart = 0, iEnd = 1;

                                      if (b\[start\].t < b\[start\].t2) {  // Change position if finished
                                          int sOld = start;   // Previous start pos
                                          start = (start+1)%CURVES;
                                          end = (end+1)%CURVES;
                                          float distX=b\[sOld\].P2.x-b\[sOld\].P1.x, distY=b\[sOld\].P2.y-b\[sOld\].P1.y;
                                          b\[start\].P0.x = b\[sOld\].P2.x;       b\[start\].P0.y = b\[sOld\].P2.y;
                                          b\[start\].P1.x = b\[sOld\].P2.x+distX; b\[start\].P1.x = b\[sOld\].P2.y+distY;
                                          b\[start\].P2.x = (RAND-0.5f);        b\[start\].P2.y = (RAND-0.5f);
                                      }
                                      
                                      for (int j = 0; j < CURVES; j++) {
                                          if (j == start) {  iStart = 0;  iEnd = bezTime; }
                                          else if (j == end){ iStart = bezTime; iEnd = 1; }
                                          else { iStart = 0; iEnd = 1; }
                                      
                                          glBegin(GL\_LINE\_STRIP);
                                          for (float i = iStart; i < iEnd; i += 0.01f) {
                                              calculateBezier(b\[j\], i);
                                              glVertex2f(b\[j\].x, b\[j\].y);
                                          }
                                          glEnd();
                                      }
                                      

                                      }

                                      Can someone figure out what's going on. Thanx

                                      U Offline
                                      U Offline
                                      User 12349108
                                      wrote on last edited by
                                      #18

                                      thanks: https://movied.org

                                      1 Reply Last reply
                                      0
                                      Reply
                                      • Reply as topic
                                      Log in to reply
                                      • Oldest to Newest
                                      • Newest to Oldest
                                      • Most Votes


                                      • Login

                                      • Don't have an account? Register

                                      • Login or register to search.
                                      • First post
                                        Last post
                                      0
                                      • Categories
                                      • Recent
                                      • Tags
                                      • Popular
                                      • World
                                      • Users
                                      • Groups