- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
虽然这是一个简单的问题,但我可能以错误的方式思考,因此找不到正确的方法。
假设我有一条线,比如第 1 行,从 m_StartPoint1 开始到 m_EndPoint1 结束。我想画另一条线,比如线 2,从 m_EndPoint1 开始,与线 1 的 alpha 角度恒定。基本上我的目标是画一个箭头。
我正在使用以下代码来计算第 2 行的 x、y 坐标。
const float ARROW_ANGLE=-PI/8.0;
wxPoint p;
p.x=m_EndPoint.x+ARROW_LENGTH*sin(ARROW_ANGLE);
p.y=m_EndPoint.y+ARROW_LENGTH*cos(ARROW_ANGLE);
m_ArrowHead1=new CLine(m_EndPoint,p,color,PenWidth); //Draws a line from m_EndPoint to p
当直线 1 的角度小于 90(以度为单位)时,此计算效果很好。但是,当线 1 的角度发生变化时,箭头显示不正确。基本上,用户应该能够按照他/她的意愿绘制线 1,并且无论线 1 的角度如何,箭头线都应该正确显示。
我已经将第 1 行表示为 vector ,并通过以下代码获得了它的角度:
class CVector2D
{
wxPoint m_StartPoint, m_EndPoint;
public:
CVector2D():m_StartPoint(),m_EndPoint() {}
CVector2D(wxPoint p1, wxPoint p2):m_StartPoint(p1),m_EndPoint(p2) {}
float GetSlope(void)
{
return float(m_EndPoint.y-m_StartPoint.y)/float(m_EndPoint.x-m_StartPoint.x);
}
float GetSlopeAngleInRadians()
{
/*Will return the angle of the vector in radians
* The angle is the counterclockwise rotation therefore it is negative
*/
float slope=GetSlope();
float InRadians=atan2(float(m_EndPoint.y-m_StartPoint.y),float(m_EndPoint.x-m_StartPoint.x));
if(InRadians<=0) return InRadians;
return -(2*PI-InRadians);
}
};
然后我尝试用下面的代码计算:
CVector2D vector(m_StartPoint,m_EndPoint);
float vector_angle=vector.GetSlopeAngleInRadians();
float total_angle=vector_angle+ARROW_ANGLE;
wxPoint p;
p.x=m_EndPoint.x+ARROW_LENGTH*cos(total_angle);
p.y=m_EndPoint.y+ARROW_LENGTH*sin(total_angle);
m_ArrowHead1=new CLine(m_EndPoint,p,color,PenWidth);
但是,这段代码也不起作用。任何想法将不胜感激。
最佳答案
这里的困难可能更多是关于数学而不是编程。但是,您需要小心处理奇点。
如何防止在 90 度处被零除:
float GetSlope(void)
{
return float(m_EndPoint.y-m_StartPoint.y)/float(m_EndPoint.x-m_StartPoint.x);
}
我推荐基于点积的数学,请参阅: How to find out if the angle between two vectors is external or internal?
附言为什么不用 double 而不是 float?
关于c++ - 一条线相对于另一条线的角度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18942907/
如何迭代(一行)分割函数给我的每个类? 我试过这个: 编辑(抱歉) $("p").attr("class").split(' ').each (function (i,n){alert(n)}
我有一条垂直线和一条水平线,当我动态调整我的 Canvas 父级时,我想调整它们的大小。 (地标) 我希望水平线始终距 Canvas 的左右边界 25 处,距底部边界 13 处。 垂直线也是如此,距上
我有一个 y 变量,我试图在图形的顶部和底部针对两个相关的 x 轴绘制它(例如 y="立方体中的事物数",x1="立方体的边长", x2="立方体的体积")。我在 numpy 数组中有 y、x1、x2
我想画一条简单的水平线,并在这条线 flex 的地方制作动画。我有这个动画的视频。你能给我一些建议如何开始以及我必须使用哪个 js/css 吗? 都是关于矩形底部的线: http://www.stop
我是一名优秀的程序员,十分优秀!