[英]Function to draw a rectangle with C++
I try to write a function that generates a rectangle which takes as input the following information: 我尝试编写一个生成矩形的函数,该矩形将以下信息作为输入:
topLeftPoint , and (width and height) of the desired rectangle. topLeftPoint和所需矩形的(宽度和高度) 。 As the result it should return the coresponding vertices.
结果,它应该返回对应的顶点。 I'm using the
OpenGL
coordinate system. 我正在使用
OpenGL
坐标系。
topLeftPoint can be a simple struct of two ints
or Vec2
. topLeftPoint可以是两个
ints
或Vec2
的简单结构。
Here is an example of how I try to solve this task: example 这是我尝试解决此任务的示例 : 示例
float verticesOfRectangle[] =
{
in.x(), in.y(),
in.x(), in.y() - h,
in.x() + w, in.y()-h,
in.x() + w, in.y(),
}
What is wrong with my code? 我的代码有什么问题? My final shapes doesn't look like a rectangle.
我的最终形状看起来不像矩形。 It looks more likes this: final shape
看起来更像这样: 最终形状
Guessing, reorder them like this: 猜测,像这样重新排列它们:
float verticesOfRectangle[] =
{
in.x(), in.y() - h,
in.x() + w, in.y() - h,
in.x(), in.y(),
in.x() + w, in.y(),
}
This should help you: 这应该可以帮助您:
void glRecti(GLint x1, GLint y1, GLint x1+w, GLint y1-h);
The vertices are the two diagonal endpoints of the rectangle.
顶点是矩形的两个对角线端点。
(x1,y1)
is the left topmost point while(x1+w, y1-h)
is the right bottom most point, withw
as the width andh
as the height of the rectangle(x1,y1)
是最左端的点,而(x1+w, y1-h)
是最右端的点,其中w
为矩形的宽度,h
为矩形的高度
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.