문제 설명
원의 둘레에 스프라이트 추가 (Add a sprite on circumference of a circle)
원의 둘레에 무작위로 스프라이트를 생성하고 싶지만 몇 시간 동안 조사한 후에도 해결책을 찾지 못했습니다. 그것이 내가 지금까지 할 수 있는 일입니다.
이 공식을 사용했습니다.
Sprite * pin = Sprite::create("pin.png");
pin‑>setPosition(Vec2((_circle‑>getContentSize().width/2)*(0.7/3), _circle‑>getContentSize().height*0.7));
Sprite * pin2 = Sprite::create("pin.png");
pin2‑>setPosition(Vec2((_circle‑>getContentSize().width/2)*(0.6/3), _circle‑>getContentSize().height*0.6));
Sprite * pin3 = Sprite::create("pin.png");
pin3‑>setPosition(Vec2((_circle‑>getContentSize().width/2)*(0.8/3), _circle‑>getContentSize().height*0.8));
Sprite * pin4 = Sprite::create("pin.png");
pin4‑>setPosition(Vec2((_circle‑>getContentSize().width/2)*(0.9/3), _circle‑>getContentSize().height*0.9));
Sprite * pin5 = Sprite::create("pin.png");
pin5‑>setPosition(Vec2((_circle‑>getContentSize().width/2)*(1/3), _circle‑>getContentSize().height));
_circle‑>addChild(pin);
_circle‑>addChild(pin2);
_circle‑>addChild(pin3);
_circle‑>addChild(pin4);
_circle‑>addChild(pin5);
그러나 나는 그런 것을 원합니다(샘플 이미지에서는 할 수 없는 정확한 각도로)
정확한 해결책을 제시해 주십시오. 시간 내주셔서 감사합니다!
참조 솔루션
방법 1:
First, it's "circumference" not "circumstance" (that will help with your searches)
Second, you are using the size of the image, not the circle inside the image.
Third, you will need to use basic trigonometry for the solution. Determining points on a circle require the use of sin
and cos
functions. After you find the center of the circle and it's radius, these should be easy to calculate with just a little bit of research.
방법 2:
Basic trig stuff ‑‑ sin
and cos
are your friends.
Example:
const float circle_x = ...;
const float circle_y = ...;
const float circle_radius = ...;
const float angle = ...;
const float x = cos(angle)*circle_radius + circle_x;
const float y = sin(angle)*circle_radius + circle_y;
// Draw stuff at (x, y).
(by Nargis、Jim、user4842163)