How to know if Offset is inside circle











up vote
0
down vote

favorite












In my code I have CircleAvatar with border. I want to know if user tap exactly on border. For this I need to check if tap is inside big circle (Container), but not inside small circle (CircleAvatar).
Does anybody know how could I check this?



Widget build(BuildContext context) {
return Listener(
child: Container(
key: key,
padding: EdgeInsets.all(8.0),
decoration: ShapeDecoration(shape: CircleBorder(), color: Colors.yellow),
child: CircleAvatar(
backgroundImage: NetworkImage(widget.imgSrc),
radius: 60.0,
),
),
onPointerDown: (event) {
if (renderBox == null) {
renderBox = key.currentContext?.findRenderObject();
}
Rect rect = renderBox.paintBounds;
// todo ......
},
);
}









share|improve this question


























    up vote
    0
    down vote

    favorite












    In my code I have CircleAvatar with border. I want to know if user tap exactly on border. For this I need to check if tap is inside big circle (Container), but not inside small circle (CircleAvatar).
    Does anybody know how could I check this?



    Widget build(BuildContext context) {
    return Listener(
    child: Container(
    key: key,
    padding: EdgeInsets.all(8.0),
    decoration: ShapeDecoration(shape: CircleBorder(), color: Colors.yellow),
    child: CircleAvatar(
    backgroundImage: NetworkImage(widget.imgSrc),
    radius: 60.0,
    ),
    ),
    onPointerDown: (event) {
    if (renderBox == null) {
    renderBox = key.currentContext?.findRenderObject();
    }
    Rect rect = renderBox.paintBounds;
    // todo ......
    },
    );
    }









    share|improve this question
























      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      In my code I have CircleAvatar with border. I want to know if user tap exactly on border. For this I need to check if tap is inside big circle (Container), but not inside small circle (CircleAvatar).
      Does anybody know how could I check this?



      Widget build(BuildContext context) {
      return Listener(
      child: Container(
      key: key,
      padding: EdgeInsets.all(8.0),
      decoration: ShapeDecoration(shape: CircleBorder(), color: Colors.yellow),
      child: CircleAvatar(
      backgroundImage: NetworkImage(widget.imgSrc),
      radius: 60.0,
      ),
      ),
      onPointerDown: (event) {
      if (renderBox == null) {
      renderBox = key.currentContext?.findRenderObject();
      }
      Rect rect = renderBox.paintBounds;
      // todo ......
      },
      );
      }









      share|improve this question













      In my code I have CircleAvatar with border. I want to know if user tap exactly on border. For this I need to check if tap is inside big circle (Container), but not inside small circle (CircleAvatar).
      Does anybody know how could I check this?



      Widget build(BuildContext context) {
      return Listener(
      child: Container(
      key: key,
      padding: EdgeInsets.all(8.0),
      decoration: ShapeDecoration(shape: CircleBorder(), color: Colors.yellow),
      child: CircleAvatar(
      backgroundImage: NetworkImage(widget.imgSrc),
      radius: 60.0,
      ),
      ),
      onPointerDown: (event) {
      if (renderBox == null) {
      renderBox = key.currentContext?.findRenderObject();
      }
      Rect rect = renderBox.paintBounds;
      // todo ......
      },
      );
      }






      flutter






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 7 at 13:10









      Andrey Turkovsky

      1,6431618




      1,6431618
























          2 Answers
          2






          active

          oldest

          votes

















          up vote
          1
          down vote













          You should use the Pythagore theorem to check if the Offset is in a circle :



          import 'dart:math';

          /// check if a [point] is in a circle of a given [radius]
          bool isPointInside(Offset point, double radius) =>
          pow(point.dx, 2) + pow(point.dy, 2) < pow(radius, 2);


          Do this for the big and small circle and check if the results match what you want.



          Edit : removed the sqrt in favour of pow(radius) as suggested by @randal-schwartz






          share|improve this answer























          • No point in using sqrt... just square both sides! pow(point.dx, 2) + pow(point.dy, 2) < pow(radius, 2)
            – Randal Schwartz
            Nov 8 at 0:41










          • You are absolutely right !
            – Muldec
            Nov 8 at 8:41










          • Thanks, but I have Offset with absolute coordinates. So for this method I have to consider this and change dx and dy. Without it method always returns false. I tried to find something like method contains(Offset offset) of Rect class.
            – Andrey Turkovsky
            Nov 8 at 14:43










          • If you know the absolute offset of the center of the circle, you can substract it to the offset of the tap and the method will work.
            – Muldec
            Nov 8 at 14:50


















          up vote
          0
          down vote



          accepted










          Maybe it's not the best way, but I've found some solution



          Rect bigRect = renderBox.paintBounds.shift(renderBox?.localToGlobal(Offset.zero));
          Rect smallRect = bigRect.deflate(padding);
          Path path = Path()
          ..fillType = PathFillType.evenOdd
          ..addOval(bigRect)
          ..addOval(smallRect);
          if (path.contains(event.position)) { // todo...


          P.S. Muldec's solution can be used too, it works, but I was looking for another type, like contains






          share|improve this answer























            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53190140%2fhow-to-know-if-offset-is-inside-circle%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            1
            down vote













            You should use the Pythagore theorem to check if the Offset is in a circle :



            import 'dart:math';

            /// check if a [point] is in a circle of a given [radius]
            bool isPointInside(Offset point, double radius) =>
            pow(point.dx, 2) + pow(point.dy, 2) < pow(radius, 2);


            Do this for the big and small circle and check if the results match what you want.



            Edit : removed the sqrt in favour of pow(radius) as suggested by @randal-schwartz






            share|improve this answer























            • No point in using sqrt... just square both sides! pow(point.dx, 2) + pow(point.dy, 2) < pow(radius, 2)
              – Randal Schwartz
              Nov 8 at 0:41










            • You are absolutely right !
              – Muldec
              Nov 8 at 8:41










            • Thanks, but I have Offset with absolute coordinates. So for this method I have to consider this and change dx and dy. Without it method always returns false. I tried to find something like method contains(Offset offset) of Rect class.
              – Andrey Turkovsky
              Nov 8 at 14:43










            • If you know the absolute offset of the center of the circle, you can substract it to the offset of the tap and the method will work.
              – Muldec
              Nov 8 at 14:50















            up vote
            1
            down vote













            You should use the Pythagore theorem to check if the Offset is in a circle :



            import 'dart:math';

            /// check if a [point] is in a circle of a given [radius]
            bool isPointInside(Offset point, double radius) =>
            pow(point.dx, 2) + pow(point.dy, 2) < pow(radius, 2);


            Do this for the big and small circle and check if the results match what you want.



            Edit : removed the sqrt in favour of pow(radius) as suggested by @randal-schwartz






            share|improve this answer























            • No point in using sqrt... just square both sides! pow(point.dx, 2) + pow(point.dy, 2) < pow(radius, 2)
              – Randal Schwartz
              Nov 8 at 0:41










            • You are absolutely right !
              – Muldec
              Nov 8 at 8:41










            • Thanks, but I have Offset with absolute coordinates. So for this method I have to consider this and change dx and dy. Without it method always returns false. I tried to find something like method contains(Offset offset) of Rect class.
              – Andrey Turkovsky
              Nov 8 at 14:43










            • If you know the absolute offset of the center of the circle, you can substract it to the offset of the tap and the method will work.
              – Muldec
              Nov 8 at 14:50













            up vote
            1
            down vote










            up vote
            1
            down vote









            You should use the Pythagore theorem to check if the Offset is in a circle :



            import 'dart:math';

            /// check if a [point] is in a circle of a given [radius]
            bool isPointInside(Offset point, double radius) =>
            pow(point.dx, 2) + pow(point.dy, 2) < pow(radius, 2);


            Do this for the big and small circle and check if the results match what you want.



            Edit : removed the sqrt in favour of pow(radius) as suggested by @randal-schwartz






            share|improve this answer














            You should use the Pythagore theorem to check if the Offset is in a circle :



            import 'dart:math';

            /// check if a [point] is in a circle of a given [radius]
            bool isPointInside(Offset point, double radius) =>
            pow(point.dx, 2) + pow(point.dy, 2) < pow(radius, 2);


            Do this for the big and small circle and check if the results match what you want.



            Edit : removed the sqrt in favour of pow(radius) as suggested by @randal-schwartz







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Nov 8 at 8:42

























            answered Nov 7 at 17:04









            Muldec

            3688




            3688












            • No point in using sqrt... just square both sides! pow(point.dx, 2) + pow(point.dy, 2) < pow(radius, 2)
              – Randal Schwartz
              Nov 8 at 0:41










            • You are absolutely right !
              – Muldec
              Nov 8 at 8:41










            • Thanks, but I have Offset with absolute coordinates. So for this method I have to consider this and change dx and dy. Without it method always returns false. I tried to find something like method contains(Offset offset) of Rect class.
              – Andrey Turkovsky
              Nov 8 at 14:43










            • If you know the absolute offset of the center of the circle, you can substract it to the offset of the tap and the method will work.
              – Muldec
              Nov 8 at 14:50


















            • No point in using sqrt... just square both sides! pow(point.dx, 2) + pow(point.dy, 2) < pow(radius, 2)
              – Randal Schwartz
              Nov 8 at 0:41










            • You are absolutely right !
              – Muldec
              Nov 8 at 8:41










            • Thanks, but I have Offset with absolute coordinates. So for this method I have to consider this and change dx and dy. Without it method always returns false. I tried to find something like method contains(Offset offset) of Rect class.
              – Andrey Turkovsky
              Nov 8 at 14:43










            • If you know the absolute offset of the center of the circle, you can substract it to the offset of the tap and the method will work.
              – Muldec
              Nov 8 at 14:50
















            No point in using sqrt... just square both sides! pow(point.dx, 2) + pow(point.dy, 2) < pow(radius, 2)
            – Randal Schwartz
            Nov 8 at 0:41




            No point in using sqrt... just square both sides! pow(point.dx, 2) + pow(point.dy, 2) < pow(radius, 2)
            – Randal Schwartz
            Nov 8 at 0:41












            You are absolutely right !
            – Muldec
            Nov 8 at 8:41




            You are absolutely right !
            – Muldec
            Nov 8 at 8:41












            Thanks, but I have Offset with absolute coordinates. So for this method I have to consider this and change dx and dy. Without it method always returns false. I tried to find something like method contains(Offset offset) of Rect class.
            – Andrey Turkovsky
            Nov 8 at 14:43




            Thanks, but I have Offset with absolute coordinates. So for this method I have to consider this and change dx and dy. Without it method always returns false. I tried to find something like method contains(Offset offset) of Rect class.
            – Andrey Turkovsky
            Nov 8 at 14:43












            If you know the absolute offset of the center of the circle, you can substract it to the offset of the tap and the method will work.
            – Muldec
            Nov 8 at 14:50




            If you know the absolute offset of the center of the circle, you can substract it to the offset of the tap and the method will work.
            – Muldec
            Nov 8 at 14:50












            up vote
            0
            down vote



            accepted










            Maybe it's not the best way, but I've found some solution



            Rect bigRect = renderBox.paintBounds.shift(renderBox?.localToGlobal(Offset.zero));
            Rect smallRect = bigRect.deflate(padding);
            Path path = Path()
            ..fillType = PathFillType.evenOdd
            ..addOval(bigRect)
            ..addOval(smallRect);
            if (path.contains(event.position)) { // todo...


            P.S. Muldec's solution can be used too, it works, but I was looking for another type, like contains






            share|improve this answer



























              up vote
              0
              down vote



              accepted










              Maybe it's not the best way, but I've found some solution



              Rect bigRect = renderBox.paintBounds.shift(renderBox?.localToGlobal(Offset.zero));
              Rect smallRect = bigRect.deflate(padding);
              Path path = Path()
              ..fillType = PathFillType.evenOdd
              ..addOval(bigRect)
              ..addOval(smallRect);
              if (path.contains(event.position)) { // todo...


              P.S. Muldec's solution can be used too, it works, but I was looking for another type, like contains






              share|improve this answer

























                up vote
                0
                down vote



                accepted







                up vote
                0
                down vote



                accepted






                Maybe it's not the best way, but I've found some solution



                Rect bigRect = renderBox.paintBounds.shift(renderBox?.localToGlobal(Offset.zero));
                Rect smallRect = bigRect.deflate(padding);
                Path path = Path()
                ..fillType = PathFillType.evenOdd
                ..addOval(bigRect)
                ..addOval(smallRect);
                if (path.contains(event.position)) { // todo...


                P.S. Muldec's solution can be used too, it works, but I was looking for another type, like contains






                share|improve this answer














                Maybe it's not the best way, but I've found some solution



                Rect bigRect = renderBox.paintBounds.shift(renderBox?.localToGlobal(Offset.zero));
                Rect smallRect = bigRect.deflate(padding);
                Path path = Path()
                ..fillType = PathFillType.evenOdd
                ..addOval(bigRect)
                ..addOval(smallRect);
                if (path.contains(event.position)) { // todo...


                P.S. Muldec's solution can be used too, it works, but I was looking for another type, like contains







                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Nov 9 at 12:42

























                answered Nov 8 at 14:45









                Andrey Turkovsky

                1,6431618




                1,6431618






























                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.





                    Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                    Please pay close attention to the following guidance:


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53190140%2fhow-to-know-if-offset-is-inside-circle%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    這個網誌中的熱門文章

                    Academy of Television Arts & Sciences

                    L'Équipe

                    1995 France bombings