{"id":482,"date":"2021-07-03T11:03:16","date_gmt":"2021-07-03T03:03:16","guid":{"rendered":"https:\/\/swordofmorning.com\/?p=482"},"modified":"2025-10-09T13:56:04","modified_gmt":"2025-10-09T05:56:04","slug":"swust-computer-graphics-experiment-1-drawing-a-straight-line-with-arbitrary-slope","status":"publish","type":"post","link":"https:\/\/swordofmorning.com\/index.php\/2021\/07\/03\/swust-computer-graphics-experiment-1-drawing-a-straight-line-with-arbitrary-slope\/","title":{"rendered":"SWUST \u8ba1\u7b97\u673a\u56fe\u5f62\u5b66 \u5b9e\u9a8c\u4e00 \u7ed8\u5236\u4efb\u610f\u659c\u7387\u76f4\u7ebf"},"content":{"rendered":"<p><div class=\"has-toc have-toc\"><\/div><\/p>\n<h2>\u4e00\u3001\u9f20\u6807\u5904\u7406\u51fd\u6570<\/h2>\n<pre><code class=\"language-cpp\">void MouseClick(int button, int state, int x, int y)\n{\n    \/\/\u5de6\u952e\u6309\u4e0b\u4e3a\u8d77\u70b9\uff0c\u5de6\u952e\u62ac\u8d77\u4e3a\u7ec8\u70b9\n    if (state == GLUT_DOWN &amp;&amp; button == GLUT_LEFT_BUTTON)\n    {\n        y = HEIGHT - y;\n        FirstPoint.x = x;\n        FirstPoint.y = y;\n    }\n    else if(state == GLUT_UP &amp;&amp; button == GLUT_LEFT_BUTTON)\n    {\n        y = HEIGHT - y;\n        SecondPoint.x = x;\n        SecondPoint.y = y;\n        DrawLine();\n    }\n}<\/code><\/pre>\n<p>&emsp;&emsp;\u8fd9\u91cc\u8981\u6c42\u5de6\u952e\u6309\u4e0b\u53bb\u5230\u62ac\u8d77\u6765\u4e4b\u95f4\u753b\u4e00\u6761\u76f4\u7ebf\u3002\u8fd9\u91cc\u518d\u914d\u5408glutMouseFunc(MouseClick)\u4f7f\u7528\u5373\u53ef\u3002<\/p>\n<h2>\u4e8c\u3001\u83b7\u53d6\u9f20\u6807\u7684\u5c4f\u5e55\u5750\u6807<\/h2>\n<pre><code class=\"language-cpp\">void MouseMove(int x, int y)\n{\n    std::cout &lt;&lt; &quot;(&quot; &lt;&lt; x &lt;&lt; &quot;, &quot; &lt;&lt; y &lt;&lt; &quot;)&quot; &lt;&lt; std::endl;\n}<\/code><\/pre>\n<p>&emsp;&emsp;\u8fd9\u91cc\u642d\u914dglutMotionFunc(MouseMove)\u4f7f\u7528\uff0c\u4e0d\u8fc7\u53ea\u8bfb\u53d6\u9f20\u6807\u6309\u952e\u6309\u4e0b\u540e\u7684\u5750\u6807\u3002<\/p>\n<h2>\u4e09\u3001\u5b8c\u6574\u4ee3\u7801<\/h2>\n<pre><code class=\"language-cpp\">#include &lt;GL\/glut.h&gt;\n#include &lt;iostream&gt;\n#include &lt;glm\/glm.hpp&gt;\n\n\/*---\u5e38\u91cf---*\/\nconst int HEIGHT(400);\nconst int WIDTH(400);\n\n\/*---\u53d8\u91cf---*\/\nglm::vec2 FirstPoint{ 0, 0 };\nglm::vec2 SecondPoint{ 0, 0 };\n\/\/\u753b\u7ebf\u8bb0\u5f55\u4e24\u70b9\n\n\/*---\u51fd\u6570---*\/\nvoid Display();\n\/\/\u663e\u793a\u51fd\u6570\nvoid Reshape(int, int);\n\/\/\u6295\u5f71\u53d8\u6362\u51fd\u6570\nvoid DrawLine();\n\/\/\u753b\u7ebf\u51fd\u6570\nvoid MouseClick(int, int, int, int);\n\/\/\u9f20\u6807\u70b9\u51fb\u51fd\u6570\nvoid MouseMove(int, int);\n\/\/\u9f20\u6807\u5750\u6807\u51fd\u6570\n\n\/*---\u4e3b\u51fd\u6570---*\/\nint main(int argc, char** argv)\n{\n    glutInit(&amp;argc, argv);\n    \/\/\u5bf9GLUT\u8fdb\u884c\u521d\u59cb\u5316\n\n    glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);\n    \/\/\u8bbe\u7f6e\u663e\u793a\u7a97\u53e3\u7f13\u5b58\u548c\u8272\u5f69\u6a21\u578b\n    \/\/\u8fd9\u91cc\u4f7f\u7528\u5355\u7f13\u5b58\u548cRGB\u989c\u8272\u6a21\u7ec4\n\n    glutInitWindowSize(WIDTH, HEIGHT);\n    glutInitWindowPosition(50, 100);\n    \/\/\u7a97\u53e3\u5927\u5c0f\u548c\u4f4d\u7f6e\u521d\u59cb\u5316\n\n    glClearColor(0, 0, 0, 1);\n    \/\/\u7a97\u53e3\u989c\u8272\n\n    glutCreateWindow(&quot;Draw a line&quot;);\n    \/\/\u7a97\u53e3\u540d\n\n    glutDisplayFunc(Display);\n    glutMotionFunc(MouseMove);\n    glutMouseFunc(MouseClick);\n    glutReshapeFunc(Reshape);\n\n    glutMainLoop();\n}\n\nvoid Display()\n{\n    glClear(GL_COLOR_BUFFER_BIT);\n}\n\n\/*--\u6295\u5f71\u51fd\u6570---*\/\nvoid Reshape(int w, int h)\n{\n    glMatrixMode(GL_PROJECTION);\n    glLoadIdentity();\n    gluOrtho2D(0, WIDTH, 0, HEIGHT);\n}\n\n\/*---\u753b\u7ebf\u51fd\u6570---*\/\nvoid DrawLine()\n{\n    glColor3f(0, 1.0, 1.0);\n    \/\/\u989c\u8272\u9009\u7684cyan\n\n    glClear(GL_COLOR_BUFFER_BIT);\n    \/\/\u5237\u65b0\u7f13\u51b2\u533a\n\n    glLineWidth(5);\n    \/\/\u7ebf\u6761\u5bbd\u5ea6\uff1a5\u4e2a\u50cf\u7d20\n\n    glBegin(GL_LINES);\n    glVertex2i(FirstPoint.x, FirstPoint.y);\n    glVertex2i(SecondPoint.x, SecondPoint.y);\n    glEnd();\n    \/\/\u753b\u7ebf\n\n    FirstPoint = glm::vec2{ 0, 0 };\n    SecondPoint = glm::vec2{ 0,0 };\n    \/\/clear\n\n    glFlush();\n    \/\/\u5237\u65b0\u7f13\u51b2\u533a\n}\n\n\/*--\u9f20\u6807\u5904\u7406\u51fd\u6570--*\/\nvoid MouseClick(int button, int state, int x, int y)\n{\n    \/\/\u5de6\u952e\u6309\u4e0b\u4e3a\u8d77\u70b9\uff0c\u5de6\u952e\u62ac\u8d77\u4e3a\u7ec8\u70b9\n    if (state == GLUT_DOWN &amp;&amp; button == GLUT_LEFT_BUTTON)\n    {\n        y = HEIGHT - y;\n        FirstPoint.x = x;\n        FirstPoint.y = y;\n    }\n    else if(state == GLUT_UP &amp;&amp; button == GLUT_LEFT_BUTTON)\n    {\n        y = HEIGHT - y;\n        SecondPoint.x = x;\n        SecondPoint.y = y;\n        DrawLine();\n    }\n}\n\n\/*---\u9f20\u6807\u5750\u6807\u51fd\u6570---*\/\nvoid MouseMove(int x, int y)\n{\n    std::cout &lt;&lt; &quot;(&quot; &lt;&lt; x &lt;&lt; &quot;, &quot; &lt;&lt; y &lt;&lt; &quot;)&quot; &lt;&lt; std::endl;\n}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u4e00\u3001\u9f20\u6807\u5904\u7406\u51fd\u6570 void MouseClick(int button, int state, int x, int y) { \/ &#8230;<\/p>","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[62],"tags":[238,237],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/swordofmorning.com\/index.php\/wp-json\/wp\/v2\/posts\/482"}],"collection":[{"href":"https:\/\/swordofmorning.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/swordofmorning.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/swordofmorning.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/swordofmorning.com\/index.php\/wp-json\/wp\/v2\/comments?post=482"}],"version-history":[{"count":1,"href":"https:\/\/swordofmorning.com\/index.php\/wp-json\/wp\/v2\/posts\/482\/revisions"}],"predecessor-version":[{"id":483,"href":"https:\/\/swordofmorning.com\/index.php\/wp-json\/wp\/v2\/posts\/482\/revisions\/483"}],"wp:attachment":[{"href":"https:\/\/swordofmorning.com\/index.php\/wp-json\/wp\/v2\/media?parent=482"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/swordofmorning.com\/index.php\/wp-json\/wp\/v2\/categories?post=482"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/swordofmorning.com\/index.php\/wp-json\/wp\/v2\/tags?post=482"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}