void CMFCGLSetupView::OnIterAdd() { iter += 1; oldx = initX; oldy = initY; InvalidateRect(0, FALSE); // TODO: 在此添加命令处理程序代码 } void CMFCGLSetupView::OnIterDec() { iter -= 1; oldx = initX; oldy = initY; if (iter < 0) { iter = 0; } InvalidateRect(0, FALSE); // TODO: 在此添加命令处理程序代码 }
时间: 2024-04-03 10:35:56 浏览: 14
这段代码是MFC框架下的命令处理函数,用于响应用户在界面上点击增加或减少迭代次数的按钮。具体来说,当用户点击增加迭代次数的按钮时,`OnIterAdd`函数会将`iter`变量加1,然后重新从初始点开始绘制Koch曲线,并且使窗口客户区域无效,强制窗口重绘。当用户点击减少迭代次数的按钮时,`OnIterDec`函数会将`iter`变量减1,然后同样重新绘制Koch曲线并使窗口客户区域无效。如果`iter`小于0,则将其置为0。
这两个函数中,`InvalidateRect`函数的作用是使窗口客户区域无效并强制窗口重绘,从而实现Koch曲线的动态更新。在调用`InvalidateRect`函数之后,由于窗口客户区域已经无效,因此Windows系统会在下一次绘制窗口时调用`OnDraw`函数重新绘制Koch曲线。
相关问题
GLfloat initX = 0, initY = 0; GLfloat oldx = 0, oldy = 0; int times = 0; bool gDrawline = false; void drawkoch(GLfloat dir, GLfloat len, GLint iter) { GLdouble dirRad = dir * 3.1415926 / 180.f; GLfloat newX = oldx + len * cos(dirRad); GLfloat newY = oldy + len * sin(dirRad); if (iter == 0) { glVertex2f(oldx, oldy); glVertex2f(newX, newY); oldx = newX; oldy = newY; } else { iter--; len = len / 3; drawkoch(dir, len, iter); dir += 60; drawkoch(dir, len, iter); dir -= 120; drawkoch(dir, len, iter); dir += 60; drawkoch(dir, len, iter); } } typedef GLfloat point2d[2]; int iter = 0; float snowAngle = 0; point2d p = { 960, 20 }; point2d anyline[2]; point2d pp; void CMFCGLSetupView::OnDraw(CDC* pDC) { CMFCGLSetupDoc* pDoc = GetDocument(); ASSERT_VALID(pDoc); if (!pDoc) return; // TODO: add draw code for native data here wglMakeCurrent(pDC->m_hDC, m_hRC); oldx = initX; oldy = initY; glClearColor(0.0f, 0.0f, 0.0f, 0.0f); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glMatrixMode(GL_PROJECTION); glLoadIdentity(); glOrtho(0, Cx, 0, Cy, -1000, 1000); switch (drawMode) { case 0: glTranslatef(400, 450, 0); glBegin(GL_LINES); glColor3f(0.0, 1.0, 0.0); drawkoch(0, 500, iter); glEnd(); break; case 1: glTranslatef(400, 450, 0); glPushMatrix(); glTranslated(250, -125 * sqrt(3), 0); glRotated(snowAngle, 0, 1, 0); glTranslated(-250, 125 * sqrt(3), 0); glBegin(GL_LINES); glColor3f(0.0, 1.0, 0.0); drawkoch(0, 500, iter); drawkoch(-120, 500, iter); drawkoch(-240, 500, iter); glEnd(); glPopMatrix(); break; case 2: if (gDrawline) { oldx = anyline[0][0]; oldy = anyline[0][1]; point2d v; v[0] = (anyline[1][0] - anyline[0][0]); v[1] = (anyline[1][1] - anyline[0][1]); float len = sqrt(v[0] * v[0] + v[1] * v[1]); v[0] /= len; v[1] /= len; point2d n = { 1, 0 }; float cosTheta = v[0] * n[0] + v[1] * n[1]; float angle = acos(cosTheta) * 180.f / PI; if (anyline[1][1] < anyline[0][1]) { angle = -angle; } glBegin(GL_LINES); drawkoch(angle, len, iter); glEnd(); } break; default: break; } glBegin(GL_POINTS); glVertex2f(pp[0], pp[1]); glEnd(); glFlush(); wglMakeCurrent(pDC->m_hDC, NULL); }
这段代码是使用OpenGL库绘制Koch曲线的实现。具体来说,这段代码定义了一个名为`drawkoch`的函数,该函数使用递归的方式绘制Koch曲线。在`OnDraw`函数中,根据不同的`drawMode`变量值,选择不同的绘制模式来绘制Koch曲线:
- 当`drawMode`为0时,绘制一个普通的Koch曲线;
- 当`drawMode`为1时,绘制一个Koch雪花;
- 当`drawMode`为2时,绘制用户自定义的线段。
其中,绘制Koch曲线和Koch雪花的实现方式相同,都是调用`drawkoch`函数来进行递归绘制。而绘制用户自定义线段的方式则需要先计算出线段的长度和方向,然后调用`drawkoch`函数来进行绘制。
此外,代码中还定义了一些全局变量,如`initX`、`initY`、`oldx`、`oldy`等,用于记录绘制Koch曲线时的初始点和当前绘制点的坐标。还有一些辅助函数,如计算角度和距离的函数等。
#!/usr/bin/python3 stopword = '' stri = '' try: for line in iter(input, stopword): stri += line + '\n' except EOFError: pass stri = stri[0:-1] # do something... int1_count = 0 str_count = 0 other_count = 0 kong_count = 0 for i in stri: if i.isdigit(): int1_count += 1 elif i.isalnum(): str_count += 1 elif i == ' ': kong_count += 1 else: other_count += 1 print('%d spaces, %d numbers, %d letters, %d other characters.' %(kong_count, int1_count ,str_count,other_count))注释一下
这是一个Python脚本,主要功能是统计用户输入的字符串中包含的空格、数字、字母和其他字符的数量,并输出统计结果。以下是代码注释:
```python
#!/usr/bin/python3 # 声明解释器类型为Python3
stopword = '' # 停止输入的标志字符串
stri = '' # 存储用户输入的字符串
try: # 开始输入循环,直到遇到停止符为止
for line in iter(input, stopword):
stri += line + '\n'
except EOFError: # 如果输入结束,则跳过输入循环
pass
stri = stri[0:-1] # 去掉字符串末尾的换行符
# 统计空格、数字、字母和其他字符的数量
int1_count = 0 # 记录数字的数量
str_count = 0 # 记录字母的数量
other_count = 0 # 记录其他字符的数量
kong_count = 0 # 记录空格的数量
for i in stri:
if i.isdigit(): # 如果是数字,则数字数量加1
int1_count += 1
elif i.isalnum(): # 如果是字母,则字母数量加1
str_count += 1
elif i == ' ': # 如果是空格,则空格数量加1
kong_count += 1
else: # 否则是其他字符,则其他字符数量加1
other_count += 1
# 输出统计结果
print('%d spaces, %d numbers, %d letters, %d other characters.' %(kong_count, int1_count ,str_count,other_count))
```
在脚本中,我们使用`try...except`语句来实现输入循环,通过`iter()`函数来实现每次输入一行,并将输入的字符串拼接到`stri`变量中。然后,我们使用`isdigit()`和`isalnum()`方法来判断字符是数字还是字母,如果是空格则直接判断,其他字符则使用`else`语句进行判断。最后,我们使用`print()`函数输出统计结果。