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 ......
},
);
}
add a comment |
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 ......
},
);
}
add a comment |
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 ......
},
);
}
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 ......
},
);
}
asked Nov 7 at 13:10
Andrey Turkovsky
1,6431618
1,6431618
add a comment |
add a comment |
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
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 haveOffsetwith absolute coordinates. So for this method I have to consider this and changedxanddy. Without it method always returns false. I tried to find something like methodcontains(Offset offset)ofRectclass.
– 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
add a comment |
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
add a comment |
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
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 haveOffsetwith absolute coordinates. So for this method I have to consider this and changedxanddy. Without it method always returns false. I tried to find something like methodcontains(Offset offset)ofRectclass.
– 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
add a comment |
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
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 haveOffsetwith absolute coordinates. So for this method I have to consider this and changedxanddy. Without it method always returns false. I tried to find something like methodcontains(Offset offset)ofRectclass.
– 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
add a comment |
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
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
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 haveOffsetwith absolute coordinates. So for this method I have to consider this and changedxanddy. Without it method always returns false. I tried to find something like methodcontains(Offset offset)ofRectclass.
– 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
add a comment |
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 haveOffsetwith absolute coordinates. So for this method I have to consider this and changedxanddy. Without it method always returns false. I tried to find something like methodcontains(Offset offset)ofRectclass.
– 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
add a comment |
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
add a comment |
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
add a comment |
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
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
edited Nov 9 at 12:42
answered Nov 8 at 14:45
Andrey Turkovsky
1,6431618
1,6431618
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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