这C++创建三角形的代码是什么意思
what does this C++ code to create a triangle mean?
这个 c++ 代码是什么意思?
我试图弄清楚这段代码的含义,但作为编程 IV 的新手,运气不佳。这是创建等边三角形的代码。
static const float COS_60 = 0.5f;
static const float COS_30 = 0.5f * sqrt(3.f);
const float side = radius * 2.f * COS_30;
const float bottomHeight = point.getY() - COS_60 * radius;
this->vertices.push_back(Vertex(point.getX(), point.getY() + radius));
this->vertices.push_back(Vertex(point.getX() + COS_60 * side, bottomHeight));
this->vertices.push_back(Vertex(point.getX() - COS_60 * side, bottomHeight));
等边三角形具有三个等长的边。拐角的角度(在内侧)是 60 度,因此需要COS_60。碰巧的是 COS 60 = 0.5,所以程序员没有编写代码来计算它,而只是使用了这个"已知值"。
边的长度为 radius * 2.f * COS_30
(即从中心到最远角乘以 30 度的余弦距离)。COS_30恰好是sqrt(3)/2
或0.5f * sqrt(3.f)
.同样,你可以计算这个,例如使用计算器,而不是把它写成 0.5 * sqrt(3)。或者,如果你想使用C++函数,你可以使用sin(30.0f * 2.f * pi / 180.0f)
[这是因为计算机上的数学几乎总是以弧度而不是度为单位完成]。
从底部到中心的距离为 COS_60 * radius
(半径的一半)。
如果我们手动应用这个数学,给定 X、Y 中心为 400、400 和半径为 100,我们得到:
side = 100 * 2 * COS_30 => 200*0.866 = 173;
bottomheight = 400 - COS_60 * radius = 400 - 50 = 350
然后是以下坐标:
400, (400 + radius) => 400, 500
400 + COS_60 * side, 350 => 400 + 173 * 0.5, 350 => 486, 350
400 - COS_60 * side, 350 => 400 - 173 * 0.5, 350 => 314, 350
相关文章:
- 获取英特尔 Kaby Lake 架构上最后一级高速缓存未命中计数的确切代码是什么?
- 这行代码是什么意思
- 此练习的C 代码是什么
- c++ 中的代码是什么意思
- 这个代码是什么意思 std::vector<cv::P oint2f> points[2]
- 这一小堆代码是什么意思
- C++向量,这段代码是什么意思
- 在 c++ 中加减分数的代码是什么
- 验证给定C++编译器是否将源代码读取为 C++11 的快速简单代码是什么?
- DllMain 中钩子回调函数的入口代码是什么
- static_cast<T* 易失性*> - 此代码是什么意思?
- 任务杀死实用程序的退出代码是什么
- GCC 发出的 vtable 汇编代码中的那两个长代码是什么?
- 这C++代码是什么意思?'const ResTable&res = am->getResources();'
- 这个Python切片语句的C++等效代码是什么
- 在链表形成中,在开头添加变量的代码是什么
- MinGW .exe 需要一些 gcc dll 的,不管代码是什么?
- &x 输出到控制台的代码是什么?
- 类型铸造?样板这行cpp代码是什么
- 这行代码是什么意思*((int*)(0))=1;