17 Replies Latest reply on Feb 19, 2007 7:41 AM by kglad

    calculate the length of a curve  drown by the mouse?

    Level 7
      is there a function which would calculate the length of a curve (in pixels)
      drown by the mouse?


        • 1. Re: calculate the length of a curve  drown by the mouse?
          Rothrock Level 5
          How are you drawing the curve?

          If you are doing it with a bunch of lineTo() and some kind of mouse event then just record each point, figure out the distance between each, and add them up.
          • 2. Re: calculate the length of a curve  drown by the mouse?
            kglad Adobe Community Professional & MVP
            if you're using lineTo this is straight-forward. if you're using curveTo this may be difficult because i'm not sure there is a closed form equation to give the length of quadratic bezier.

            if you're using a simple mousedown start draw, mouse up stop draw you can use:

            • 3. Re: calculate the length of a curve  drown by the mouse?
              Level 7
              Thanks, kglad
              While the mouse is moving, how often onMouseMove = function() is called?

              "kglad" <webforumsuser@macromedia.com> wrote in message
              news:er345m$79m$1@forums.macromedia.com...
              > if you're using lineTo this is straight-forward. if you're using curveTo
              this
              > may be difficult because i'm not sure there is a closed form equation to
              give
              > the length of quadratic bezier.
              >
              > if you're using a simple mousedown start draw, mouse up stop draw you can
              use:
              >
              >
              >
              > this.onMouseDown = function() {
              > this.createEmptyMovieClip("mc", 1);
              > mc.lineStyle(1, 0x000000);
              > mc.moveTo(this._xmouse, this._ymouse);
              > lastX = this._xmouse;
              > lastY = this._ymouse;
              > d = 0;
              > this.onMouseMove = function() {
              > mc.lineTo(this._xmouse, this._ymouse);
              > d += dist(lastX, this._xmouse, lastY, this._ymouse);
              > lastX = this._xmouse;
              > lastY = this._ymouse;
              > };
              > };
              > this.onMouseUp = function() {
              > delete this.onMouseMove;
              > };
              > function dist(x1, x2, y1, y2) {
              > return Math.sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));
              > }
              >


              • 5. Re: calculate the length of a curve  drown by the mouse?
                Level 7
                Background to my previous question -
                when the mouse moves slowly the code seem to work correctly
                On high speeds the calculated length seems to be smaller than the drawn one.

                "aa" <A@aa.com> wrote in message news:er41b8$8l3$1@forums.macromedia.com...
                > Thanks, kglad
                > While the mouse is moving, how often onMouseMove = function() is called?


                • 6. Re: calculate the length of a curve  drown by the mouse?
                  Level 7
                  Another feature turns out:
                  If this curve is drawn on another clip i.e. mc is attached to another clip,
                  then the function returns the distance not in absolute pixels, but in
                  relative once so to say. I.e. is the clip to shich mc is attached is, say,
                  200 px wide, then a streight line from one side to the other side of this
                  clip is returned as 200.
                  If I scale the clip up, say, twofold, the line from side to side on the
                  screen is twice as long, but the function still returns 200.


                  "kglad" <webforumsuser@macromedia.com> wrote in message
                  news:er345m$79m$1@forums.macromedia.com...
                  > if you're using lineTo this is straight-forward. if you're using curveTo
                  this
                  > may be difficult because i'm not sure there is a closed form equation to
                  give
                  > the length of quadratic bezier.
                  >
                  > if you're using a simple mousedown start draw, mouse up stop draw you can
                  use:
                  >
                  >
                  >
                  > this.onMouseDown = function() {
                  > this.createEmptyMovieClip("mc", 1);
                  > mc.lineStyle(1, 0x000000);
                  > mc.moveTo(this._xmouse, this._ymouse);
                  > lastX = this._xmouse;
                  > lastY = this._ymouse;
                  > d = 0;
                  > this.onMouseMove = function() {
                  > mc.lineTo(this._xmouse, this._ymouse);
                  > d += dist(lastX, this._xmouse, lastY, this._ymouse);
                  > lastX = this._xmouse;
                  > lastY = this._ymouse;
                  > };
                  > };
                  > this.onMouseUp = function() {
                  > delete this.onMouseMove;
                  > };
                  > function dist(x1, x2, y1, y2) {
                  > return Math.sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));
                  > }
                  >


                  • 7. Re: calculate the length of a curve  drown by the mouse?
                    kglad Adobe Community Professional & MVP
                    for the distance calculation it shouldn't matter how quickly you move your mouse. onMouseMove will update as frequently as every millisecond.

                    if you draw into a scaled movieclip, you'll need to adjust the distance calculation by the scaling factors.
                    • 8. Re: calculate the length of a curve  drown by the mouse?
                      Level 7
                      The algorithm will take a shortcut between mouse positions at T and (T+1
                      millisecond) and the position of the mouse at, say, (T+0.5 millisecond) will
                      be lost which matters if the mouse trajectory is not a streight line.


                      "kglad" <webforumsuser@macromedia.com> wrote in message
                      news:er4klq$14q$1@forums.macromedia.com...
                      > for the distance calculation it shouldn't matter how quickly you move your
                      > mouse. onMouseMove will update as frequently as every millisecond.
                      >
                      > if you draw into a scaled movieclip, you'll need to adjust the distance
                      > calculation by the scaling factors.
                      >


                      • 9. Re: calculate the length of a curve  drown by the mouse?
                        kglad Adobe Community Professional & MVP
                        the algorithm takes no shortcut. if onMouseMove executes, the distance function will execute.

                        because onMouseMove doesn't execute at t+.5ms your mouse position at that time is irrelevent to the drawing on-stage and to your distance calculation. even if your mouse is at 3 different stage locations at t, t+.5ms, t+1ms a line segment drawn will be drawn directly connecting the mouse positions (rounded to whole pixels) at t and t+1ms and that distance is accurately computed.
                        • 10. Re: calculate the length of a curve  drown by the mouse?
                          Level 7
                          That is exactly what I am saying, but I draw just the opposite conclusion.
                          If for 1 ms the mouse draws a circle i.e. returns to the initail point, the
                          distance calculated will be 0, will it not?

                          "kglad" <webforumsuser@macromedia.com> wrote in message
                          news:er7ef3$ct9$1@forums.macromedia.com...
                          > the algorithm takes no shortcut. if onMouseMove executes, the distance
                          > function will execute.
                          >
                          > because onMouseMove doesn't execute at t+.5ms your mouse position at that
                          time
                          > is irrelevent to the drawing on-stage and to your distance calculation.
                          even
                          > if your mouse is at 3 different stage locations at t, t+.5ms, t+1ms a line
                          > segment drawn will be drawn directly connecting the mouse positions
                          (rounded to
                          > whole pixels) at t and t+1ms and that distance is accurately computed.
                          >


                          • 11. Re: calculate the length of a curve  drown by the mouse?
                            kglad Adobe Community Professional & MVP
                            the calculation will be zero and you will have drawn a circle with circumference zero. so, the calculation is correct.
                            • 12. Re: calculate the length of a curve  drown by the mouse?
                              Level 7
                              I am not getting you, I am afraid. The circle's circumference is zero only
                              is the radius is zero which in not the case in my example
                              "kglad" <webforumsuser@macromedia.com> wrote in message
                              news:er7qlr$r92$1@forums.macromedia.com...
                              > the calculation will be zero and you will have drawn a circle with
                              circumference zero. so, the calculation is correct.


                              • 13. Re: calculate the length of a curve  drown by the mouse?
                                kglad Adobe Community Professional & MVP
                                time t1: you start drawing a pt x1,y1 - and the onMouseDown records that

                                time t1+.5ms: you move to pt x2,y2 - onMouseMove fails to detect that

                                time t1+1ms: you move back to pt x1,y1 - onMouseMove detects that.

                                result: you've drawn nothing and the curve distance is correctly calculated to be zero.
                                • 14. Re: calculate the length of a curve  drown by the mouse?
                                  Level 7
                                  I see what you meen - thanks.
                                  This 1 ms interval - is it the same on every installation or it depends on
                                  something?
                                  I use FLASH in an application where a device is attached to computer and the
                                  mouse is controlled the signals generated by this device. The device is
                                  generating signals as short as several ms and I need to detect it, draw the
                                  curve and calculate its length. The curve is gong to be slightly bended
                                  line.

                                  "kglad" <webforumsuser@macromedia.com> wrote in message
                                  news:era68q$lon$1@forums.macromedia.com...
                                  > time t1: you start drawing a pt x1,y1 - and the onMouseDown records that
                                  >
                                  > time t1+.5ms: you move to pt x2,y2 - onMouseMove fails to detect that
                                  >
                                  > time t1+1ms: you move back to pt x1,y1 - onMouseMove detects that.
                                  >
                                  > result: you've drawn nothing and the curve distance is correctly
                                  calculated
                                  > to be zero.
                                  >


                                  • 15. Re: calculate the length of a curve  drown by the mouse?
                                    kglad Adobe Community Professional & MVP
                                    the 1 ms limit is the frequencey limit that flash can update the stage or can retrieve data (like mouse position) from the stage.

                                    if you're feeding data into flash more frequently than every ms, and don't use the stage for that feed, i don't know how frequently you can feed data into flash.