gpt4 book ai didi

c++ - 找到 2 个 vector 之间的 vector (2D)

转载 作者:塔克拉玛干 更新时间:2023-11-03 04:36:33 25 4
gpt4 key购买 nike

例如,我有以下 2 个 vector :

       *B





*A

我想要的 vector 是 C

*C       *B





*A

我想要做的是生成正方形轮廓。我使用 2d slurp:其中 v0 是 A,v2 是 B。现在我使用 sslerp2D 来制作圆边,但我也想要规则的方形边,因此我想修改它。

POINTFLOAT slerp2d( const POINTFLOAT &v0, 
const POINTFLOAT &v1, float t )
{
float dot = (v0.x * v1.x + v0.y * v1.y);

if( dot < -1.0f ) dot = -1.0f;
if( dot > 1.0f ) dot = 1.0f;

float theta_0 = acos( dot );
float theta = theta_0 * t;

POINTFLOAT v2;
v2.x = -v0.y;
v2.y = v0.x;

POINTFLOAT result;
result.x = v0.x * cos(theta) + v2.x * sin(theta);
result.y = v0.y * cos(theta) + v2.y * sin(theta);

return result;
}

谢谢

编辑:

我生成这样的轮廓:

void OGLSHAPE::GenerateLinePoly(std::vector<DOUBLEPOINT> &input, int width)
{
OutlineVec.clear();
if(input.size() < 2)
{
return;
}


if(connected)
{
input.push_back(input[0]);
input.push_back(input[1]);
}


float w = width / 2.0f;

//glBegin(GL_TRIANGLES);
for( size_t i = 0; i < input.size()-1; ++i )
{
POINTFLOAT cur;
cur.x = input[i].point[0];
cur.y = input[i].point[1];


POINTFLOAT nxt;


nxt.x = input[i+1].point[0];
nxt.y = input[i+1].point[1];

POINTFLOAT b;
b.x = nxt.x - cur.x;
b.y = nxt.y - cur.y;

b = normalize(b);



POINTFLOAT b_perp;
b_perp.x = -b.y;
b_perp.y = b.x;


POINTFLOAT p0;
POINTFLOAT p1;
POINTFLOAT p2;
POINTFLOAT p3;

p0.x = cur.x + b_perp.x * w;
p0.y = cur.y + b_perp.y * w;

p1.x = cur.x - b_perp.x * w;
p1.y = cur.y - b_perp.y * w;

p2.x = nxt.x + b_perp.x * w;
p2.y = nxt.y + b_perp.y * w;

p3.x = nxt.x - b_perp.x * w;
p3.y = nxt.y - b_perp.y * w;

OutlineVec.push_back(p0.x);
OutlineVec.push_back(p0.y);
OutlineVec.push_back(p1.x);
OutlineVec.push_back(p1.y);
OutlineVec.push_back(p2.x);
OutlineVec.push_back(p2.y);

OutlineVec.push_back(p2.x);
OutlineVec.push_back(p2.y);
OutlineVec.push_back(p1.x);
OutlineVec.push_back(p1.y);
OutlineVec.push_back(p3.x);
OutlineVec.push_back(p3.y);



// only do joins when we have a prv
if( i == 0 ) continue;


POINTFLOAT prv;
prv.x = input[i-1].point[0];
prv.y = input[i-1].point[1];

POINTFLOAT a;
a.x = prv.x - cur.x;
a.y = prv.y - cur.y;

a = normalize(a);

POINTFLOAT a_perp;
a_perp.x = a.y;
a_perp.y = -a.x;

float det = a.x * b.y - b.x * a.y;
if( det > 0 )
{
a_perp.x = -a_perp.x;
a_perp.y = -a_perp.y;

b_perp.x = -b_perp.x;
b_perp.y = -b_perp.y;
}

// TODO: do inner miter calculation

// flip around normals and calculate round join points
a_perp.x = -a_perp.x;
a_perp.y = -a_perp.y;

b_perp.x = -b_perp.x;
b_perp.y = -b_perp.y;

size_t num_pts = 4;

std::vector< POINTFLOAT> round( 1 + num_pts + 1 );
POINTFLOAT nc;
nc.x = cur.x + (a_perp.x * w);
nc.y = cur.y + (a_perp.y * w);

round.front() = nc;

nc.x = cur.x + (b_perp.x * w);
nc.y = cur.y + (b_perp.y * w);

round.back() = nc;

for( size_t j = 1; j < num_pts+1; ++j )
{
float t = (float)j/(float)(num_pts+1);
if( det > 0 )
{
POINTFLOAT nin;
nin = slerp2d( b_perp, a_perp, 1.0f-t );
nin.x *= w;
nin.y *= w;

nin.x += cur.x;
nin.y += cur.y;

round[j] = nin;
}
else
{
POINTFLOAT nin;
nin = slerp2d( a_perp, b_perp, t );
nin.x *= w;
nin.y *= w;

nin.x += cur.x;
nin.y += cur.y;

round[j] = nin;
}
}

for( size_t j = 0; j < round.size()-1; ++j )
{

OutlineVec.push_back(cur.x);
OutlineVec.push_back(cur.y);


if( det > 0 )
{
OutlineVec.push_back(round[j + 1].x);
OutlineVec.push_back(round[j + 1].y);
OutlineVec.push_back(round[j].x);
OutlineVec.push_back(round[j].y);
}
else
{

OutlineVec.push_back(round[j].x);
OutlineVec.push_back(round[j].y);

OutlineVec.push_back(round[j + 1].x);
OutlineVec.push_back(round[j + 1].y);
}
}
}

}

最佳答案

您似乎在处理点,而不是 vector 。如果你有两个点(X1,Y1),(X2,Y2),那么(X1, Y2) 和 (X2, Y1) 都应该与前两点[编辑:当然,前提是前两点不形成垂直线或水平线]。对于您提供的输入,一个是您标记为 C 的点,另一个是右下角的镜像三角形。

进一步编辑:我不太清楚你所说的不完全是 90 度的角度是什么意思。至于上面提到的原始线是垂直或水平的情况,从其中之一形成无限多种直角三角形是微不足道的:为第二条边选择任意长度并将其从两点之一延伸90度.您的第三条边(斜边)将从那个新点延伸到两个原始点的其他。如果您愿意,您还可以使用形成斜边的原始对构造无限多种其他直角三角形(尽管它涉及更难的数学 -- A2 = C2-B2,其中 C 是原始线的长度,B 是您在 0..C 范围内选择的任意长度。

关于c++ - 找到 2 个 vector 之间的 vector (2D),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3269597/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com