Обычно хранить объекты непосредственно в контейнере STL является лучшим, поскольку это является самым простым, самым эффективным, и является самым легким для использования объекта.
, Если Ваш объект сам имеет non-copyable синтаксис или является абстрактным типом, необходимо будет сохранить указатели (самый легкий, должен использовать shared_ptr)
Find the direction vector, i.e. let the position vectors be (using floats) B = (x2, y2) and A = (x1, y1), then AB = B - A. Normalize that vector by dividing by its length ( Math.Sqrt(xx + yy) ). Then multiply the direction vector AB by the original length minus the circle's radius, and add back to the lines starting position:
double dx = x2 - x1;
double dy = y2 - y1;
double length = Math.Sqrt(dx * dx + dy * dy);
if (length > 0)
{
dx /= length;
dy /= length;
}
dx *= length - radius;
dy *= length - radius;
int x3 = (int)(x1 + dx);
int y3 = (int)(y1 + dy);
Edit: Fixed the code, aaand fixed the initial explanation (thought you wanted the line to go out from the circle's center to its perimeter :P)
Не знаю, зачем вам вообще пришлось вводить круг. Для линии, простирающейся от (x2, y2)
до (x1, y1)
, вы можете вычислить любую точку на этой прямой как:
(x2+p*(x1-x2),y2+p*(y1-y2))
где p
- это процентное значение вдоль линии, по которой вы хотите перейти.
Чтобы вычислить процент, вам просто нужно:
p = r/L
Итак, в вашем случае (x3, y3)
можно рассчитать как:
(x2+(10/L)*(x1-x2),y2+(10/L)*(y1-y2))
Например, если у вас есть две точки (x2 = 1, y2 = 5)
и (x1 = -6, y1 = 22)
, они имеют длину sqrt (7 2 + 17 2 или 18,38477631 и деленное на это 10 получается 0,543928293. Подставив все эти цифры в уравнение выше:
(x2 + (10/l) * (x1-x2) , y2 + (10/l) * (y1-y2))
= (1 + 0.543928293 * (-6- 1) , 5 + 0.543928293 * (22- 5))
= (1 + 0.543928293 * -7 , 5 + 0.543928293 * 17 )
= (x3=-2.807498053,y3=14.24678098)
Расстояние между (x3, y3)
и (x1, y1)
- это sqrt (3.192501947 2 + 7.753219015 2 ) или 8.384776311,
You can use similar triangles. For the main triangle, d
is the hypotenuses and the extension of r
is the vertical line that meets the right angle. Inside the circle you will have a smaller triangle with a hypotenuses of length r
.
r/d = (x2-a0)/(x2-x1) = (y2-b0)/(y2-y1) a0 = x2 + (x2-x1)r/d b0 = y2 + (y2-y1)r/d