{"id":2452,"date":"2026-05-02T14:47:46","date_gmt":"2026-05-02T06:47:46","guid":{"rendered":"https:\/\/randengzhe.cn\/wordpress\/?p=2452"},"modified":"2026-05-15T13:01:05","modified_gmt":"2026-05-15T05:01:05","slug":"sfml-day2","status":"publish","type":"post","link":"https:\/\/randengzhe.cn\/wordpress\/2026\/05\/02\/sfml-day2\/","title":{"rendered":"Day2 SFML+\u76f4\u89d2\u5750\u6807\u7cfb+\u7f51\u683c+\u51fd\u6570sin\u56fe\u50cf"},"content":{"rendered":"\n<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_85 counter-hierarchy ez-toc-counter ez-toc-grey ez-toc-container-direction\">\r\n<div class=\"ez-toc-title-container\">\r\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Table of Contents<\/p>\r\n<span class=\"ez-toc-title-toggle\"><a href=\"#\" class=\"ez-toc-pull-right ez-toc-btn ez-toc-btn-xs ez-toc-btn-default ez-toc-toggle\" aria-label=\"Toggle Table of Content\"><span class=\"ez-toc-js-icon-con\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #999;color:#999\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewBox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #999;color:#999\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewBox=\"0 0 24 24\" version=\"1.2\" baseProfile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/span><\/a><\/span><\/div>\r\n<nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-1'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/randengzhe.cn\/wordpress\/2026\/05\/02\/sfml-day2\/#Day2_%E7%9B%AE%E6%A0%87\" >Day2 \u76ee\u6807<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-1'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/randengzhe.cn\/wordpress\/2026\/05\/02\/sfml-day2\/#%E7%9B%AE%E6%A0%87%E9%A2%84%E8%A7%88\" >\u76ee\u6807\u9884\u89c8<\/a><ul class='ez-toc-list-level-2' ><li class='ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/randengzhe.cn\/wordpress\/2026\/05\/02\/sfml-day2\/#Makefile_%E5%AF%B9%E5%BA%94%E5%8A%A0%E4%B8%80%E6%9D%A1\" >Makefile \u5bf9\u5e94\u52a0\u4e00\u6761<\/a><\/li><\/ul><\/li><li class='ez-toc-page-1 ez-toc-heading-level-1'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/randengzhe.cn\/wordpress\/2026\/05\/02\/sfml-day2\/#%E4%BD%A0%E4%BB%8A%E5%A4%A9%E8%A6%81%E7%90%86%E8%A7%A3%E7%9A%84_3_%E4%B8%AA%E5%85%B3%E9%94%AE%E7%82%B9\" >\u4f60\u4eca\u5929\u8981\u7406\u89e3\u7684 3 \u4e2a\u5173\u952e\u70b9<\/a><\/li><\/ul><\/nav><\/div>\r\n<h1 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Day2_%E7%9B%AE%E6%A0%87\"><\/span>Day2 \u76ee\u6807<span class=\"ez-toc-section-end\"><\/span><\/h1>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u753b\u6807\u51c6<strong>\u76f4\u89d2\u5750\u6807\u7cfb<\/strong>\uff08X \u8f74\u3001Y \u8f74\uff09<\/li>\n\n\n\n<li>\u753b<strong>\u7f51\u683c<\/strong>\uff08\u65b9\u4fbf\u770b\u51fd\u6570\u56fe\u50cf\uff09<\/li>\n\n\n\n<li>\u753b\u51fa\u7b2c\u4e00\u6761\u6570\u5b66\u66f2\u7ebf\uff1a<strong>y = sin(x)<\/strong><\/li>\n\n\n\n<li>\u7406\u89e3\u300c\u6570\u5b66\u5750\u6807 \u2192 \u5c4f\u5e55\u5750\u6807\u300d\u7684\u8f6c\u6362<\/li>\n<\/ul>\n\n\n\n<h1 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"%E7%9B%AE%E6%A0%87%E9%A2%84%E8%A7%88\"><\/span>\u76ee\u6807\u9884\u89c8<span class=\"ez-toc-section-end\"><\/span><\/h1>\n\n\n\n<figure class=\"wp-block-image size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"820\" height=\"655\" src=\"https:\/\/randengzhe.cn\/wordpress\/wp-content\/uploads\/2026\/05\/day2.png\" alt=\"\" class=\"wp-image-2500\" srcset=\"https:\/\/randengzhe.cn\/wordpress\/wp-content\/uploads\/2026\/05\/day2.png 820w, https:\/\/randengzhe.cn\/wordpress\/wp-content\/uploads\/2026\/05\/day2-300x240.png 300w, https:\/\/randengzhe.cn\/wordpress\/wp-content\/uploads\/2026\/05\/day2-768x613.png 768w, https:\/\/randengzhe.cn\/wordpress\/wp-content\/uploads\/2026\/05\/day2-376x300.png 376w\" sizes=\"auto, (max-width: 820px) 100vw, 820px\" \/><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">\u76f4\u63a5\u7ed9\u4f60\u5b8c\u6574\u53ef\u7f16\u8bd1\u4ee3\u7801\uff1a<br>\u6587\u4ef6\u540d\uff1a<code>day2_graph.cpp<\/code><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;SFML\/Graphics.hpp>\n#include &lt;cmath>\n\nusing namespace std;\n\nint main()\n{\n    \/\/ \u7a97\u53e3\uff1a800\u00d7600\uff0c\u4e2d\u6587\u6807\u9898\n    sf::RenderWindow window(sf::VideoMode(800, 600), L\"SFML Day2 \u00b7 \u51fd\u6570\u56fe\u50cf\");\n\n    \/\/ \u5750\u6807\u7cfb\u4e2d\u5fc3\u653e\u5728\u5c4f\u5e55\u4e2d\u5fc3\n    const float cx = 400;\n    const float cy = 300;\n\n    \/\/ \u5750\u6807\u8f74\u989c\u8272\n    sf::Color axis_color(120, 220, 255);\n    \/\/ \u7f51\u683c\u989c\u8272\n    sf::Color grid_color(60, 60, 80);\n    \/\/ \u66f2\u7ebf\u989c\u8272\n    sf::Color sin_color(255, 80, 80);\n\n    while (window.isOpen())\n    {\n        sf::Event event;\n        while (window.pollEvent(event))\n        {\n            if (event.type == sf::Event::Closed)\n                window.close();\n        }\n\n        \/\/ \u80cc\u666f\n        window.clear(sf::Color(20, 20, 30));\n\n        \/\/ ====================== \u753b\u7f51\u683c ======================\n        for (int x = 0; x &lt; 800; x += 40)\n        {\n            sf::Vertex line&#91;] = {\n                {{(float)x, 0}, grid_color},\n                {{(float)x, 600}, grid_color}\n            };\n            window.draw(line, 2, sf::Lines);\n        }\n        for (int y = 0; y &lt; 600; y += 40)\n        {\n            sf::Vertex line&#91;] = {\n                {{0, (float)y}, grid_color},\n                {{800, (float)y}, grid_color}\n            };\n            window.draw(line, 2, sf::Lines);\n        }\n\n        \/\/ ====================== \u753b\u5750\u6807\u8f74 ======================\n        sf::Vertex x_axis&#91;] = {\n            {{0, cy}, axis_color},\n            {{800, cy}, axis_color}\n        };\n        sf::Vertex y_axis&#91;] = {\n            {{cx, 0}, axis_color},\n            {{cx, 600}, axis_color}\n        };\n        window.draw(x_axis, 2, sf::Lines);\n        window.draw(y_axis, 2, sf::Lines);\n\n        \/\/ ====================== \u753b y = sin(x) ======================\n        for (int sx = -350; sx &lt; 350; ++sx)\n        {\n            \/\/ \u6570\u5b66 x\n            float x = sx \/ 50.0f;\n            \/\/ \u6570\u5b66 y = sin(x)\n            float y = sin(x);\n\n            \/\/ \u653e\u5927 100 \u500d\u65b9\u4fbf\u89c2\u5bdf\n            y *= 100;\n\n            \/\/ \u8f6c\u6362\u5230\u5c4f\u5e55\u5750\u6807\n            float px = cx + sx;\n            float py = cy - y;\n\n            \/\/ \u753b\u70b9\n            sf::CircleShape dot(1.5f);\n            dot.setPosition(px, py);\n            dot.setFillColor(sin_color);\n            window.draw(dot);\n        }\n\n        window.display();\n    }\n\n    return 0;\n}<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"Makefile_%E5%AF%B9%E5%BA%94%E5%8A%A0%E4%B8%80%E6%9D%A1\"><\/span>Makefile \u5bf9\u5e94\u52a0\u4e00\u6761<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>day2: day2_graph.o\n    $(CXX) $(CXXFLAGS) -o day2 day2_graph.o $(LIBS)<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">\u7f16\u8bd1\u8fd0\u884c\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>make day2\n.\/day2<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"%E4%BD%A0%E4%BB%8A%E5%A4%A9%E8%A6%81%E7%90%86%E8%A7%A3%E7%9A%84_3_%E4%B8%AA%E5%85%B3%E9%94%AE%E7%82%B9\"><\/span>\u4f60\u4eca\u5929\u8981\u7406\u89e3\u7684 3 \u4e2a\u5173\u952e\u70b9<span class=\"ez-toc-section-end\"><\/span><\/h1>\n\n\n\n<ol class=\"wp-block-list\">\n<li>SFML \u539f\u70b9\u5728<strong>\u5de6\u4e0a\u89d2<\/strong>\uff0c\u6570\u5b66\u539f\u70b9\u5728<strong>\u4e2d\u5fc3<\/strong>\uff0c\u6240\u4ee5\u8981 <code>cx=400, cy=300<\/code><\/li>\n\n\n\n<li>\u5c4f\u5e55 y \u5411\u4e0b\uff0c\u6570\u5b66 y \u5411\u4e0a \u2192 \u6240\u4ee5\u7528 <code>cy - y<\/code><\/li>\n\n\n\n<li>\u51fd\u6570\u56fe\u50cf\u5c31\u662f\uff1a<strong>\u5faa\u73af\u7b97\u70b9 \u2192 \u753b\u70b9 \u2192 \u8fde\u6210\u66f2\u7ebf<\/strong><\/li>\n<\/ol>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Day2 \u76ee\u6807 \u76ee\u6807\u9884\u89c8 \u76f4\u63a5\u7ed9\u4f60\u5b8c\u6574\u53ef\u7f16\u8bd1\u4ee3\u7801\uff1a &hellip;<\/p>\n","protected":false},"author":1,"featured_media":2500,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_feature_clip_id":0,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_post_was_ever_published":false},"categories":[30],"tags":[],"class_list":["post-2452","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-sfml"],"jetpack_featured_media_url":"https:\/\/randengzhe.cn\/wordpress\/wp-content\/uploads\/2026\/05\/day2.png","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/randengzhe.cn\/wordpress\/wp-json\/wp\/v2\/posts\/2452","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/randengzhe.cn\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/randengzhe.cn\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/randengzhe.cn\/wordpress\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/randengzhe.cn\/wordpress\/wp-json\/wp\/v2\/comments?post=2452"}],"version-history":[{"count":6,"href":"https:\/\/randengzhe.cn\/wordpress\/wp-json\/wp\/v2\/posts\/2452\/revisions"}],"predecessor-version":[{"id":2625,"href":"https:\/\/randengzhe.cn\/wordpress\/wp-json\/wp\/v2\/posts\/2452\/revisions\/2625"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/randengzhe.cn\/wordpress\/wp-json\/wp\/v2\/media\/2500"}],"wp:attachment":[{"href":"https:\/\/randengzhe.cn\/wordpress\/wp-json\/wp\/v2\/media?parent=2452"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/randengzhe.cn\/wordpress\/wp-json\/wp\/v2\/categories?post=2452"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/randengzhe.cn\/wordpress\/wp-json\/wp\/v2\/tags?post=2452"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}