{"id":317,"date":"2019-03-21T21:25:55","date_gmt":"2019-03-21T13:25:55","guid":{"rendered":"https:\/\/www.ccagml.com\/?p=317"},"modified":"2019-05-12T18:59:06","modified_gmt":"2019-05-12T10:59:06","slug":"%e8%af%a6%e8%a7%a3js%e5%ae%9e%e7%8e%b0%e7%ba%bf%e6%ae%b5%e4%ba%a4%e7%82%b9%e7%9a%84%e4%b8%89%e7%a7%8d%e7%ae%97%e6%b3%95","status":"publish","type":"post","link":"https:\/\/www.ccagml.com\/?p=317","title":{"rendered":"\u8be6\u89e3js\u5b9e\u73b0\u7ebf\u6bb5\u4ea4\u70b9\u7684\u4e09\u79cd\u7b97\u6cd5"},"content":{"rendered":"<p>\u65b9\u6cd5\u4e00: \u6c42\u4e24\u6761\u7ebf\u6bb5\u6240\u5728\u76f4\u7ebf\u7684\u4ea4\u70b9, \u518d\u5224\u65ad\u4ea4\u70b9\u662f\u5426\u5728\u4e24\u6761\u7ebf\u6bb5\u4e0a. <\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\"> \r\nfunction segmentsIntr(a, b, c, d){ \r\n \r\n\/** 1 \u89e3\u7ebf\u6027\u65b9\u7a0b\u7ec4, \u6c42\u7ebf\u6bb5\u4ea4\u70b9. **\/ \r\n\/\/ \u5982\u679c\u5206\u6bcd\u4e3a0 \u5219\u5e73\u884c\u6216\u5171\u7ebf, \u4e0d\u76f8\u4ea4 \r\n var denominator = (b.y - a.y)*(d.x - c.x) - (a.x - b.x)*(c.y - d.y); \r\n if (denominator==0) { \r\n return false; \r\n } \r\n \r\n\/\/ \u7ebf\u6bb5\u6240\u5728\u76f4\u7ebf\u7684\u4ea4\u70b9\u5750\u6807 (x , y) \r\n var x = ( (b.x - a.x) * (d.x - c.x) * (c.y - a.y) \r\n  + (b.y - a.y) * (d.x - c.x) * a.x \r\n  - (d.y - c.y) * (b.x - a.x) * c.x ) \/ denominator ; \r\n var y = -( (b.y - a.y) * (d.y - c.y) * (c.x - a.x) \r\n  + (b.x - a.x) * (d.y - c.y) * a.y \r\n  - (d.x - c.x) * (b.y - a.y) * c.y ) \/ denominator; \r\n \r\n\/** 2 \u5224\u65ad\u4ea4\u70b9\u662f\u5426\u5728\u4e24\u6761\u7ebf\u6bb5\u4e0a **\/ \r\n if ( \r\n \/\/ \u4ea4\u70b9\u5728\u7ebf\u6bb51\u4e0a \r\n (x - a.x) * (x - b.x) &lt;= 0 &amp;&amp; (y - a.y) * (y - b.y) &lt;= 0 \r\n \/\/ \u4e14\u4ea4\u70b9\u4e5f\u5728\u7ebf\u6bb52\u4e0a \r\n  &amp;&amp; (x - c.x) * (x - d.x) &lt;= 0 &amp;&amp; (y - c.y) * (y - d.y) &lt;= 0 \r\n ){ \r\n \r\n \/\/ \u8fd4\u56de\u4ea4\u70b9p \r\n return { \r\n  x : x, \r\n  y : y \r\n  } \r\n } \r\n \/\/\u5426\u5219\u4e0d\u76f8\u4ea4 \r\n return false \r\n \r\n} \r\n\r\n<\/pre>\n<p>\u65b9\u6cd5\u4e8c: \u5224\u65ad\u6bcf\u4e00\u6761\u7ebf\u6bb5\u7684\u4e24\u4e2a\u7aef\u70b9\u662f\u5426\u90fd\u5728\u53e6\u4e00\u6761\u7ebf\u6bb5\u7684\u4e24\u4fa7, \u662f\u5219\u6c42\u51fa\u4e24\u6761\u7ebf\u6bb5\u6240\u5728\u76f4\u7ebf\u7684\u4ea4\u70b9, \u5426\u5219\u4e0d\u76f8\u4ea4. <\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\"> \r\nfunction segmentsIntr(a, b, c, d){ \r\n \r\n \/\/\u7ebf\u6bb5ab\u7684\u6cd5\u7ebfN1 \r\n var nx1 = (b.y - a.y), ny1 = (a.x - b.x); \r\n \r\n \/\/\u7ebf\u6bb5cd\u7684\u6cd5\u7ebfN2 \r\n var nx2 = (d.y - c.y), ny2 = (c.x - d.x); \r\n \r\n \/\/\u4e24\u6761\u6cd5\u7ebf\u505a\u53c9\u4e58, \u5982\u679c\u7ed3\u679c\u4e3a0, \u8bf4\u660e\u7ebf\u6bb5ab\u548c\u7ebf\u6bb5cd\u5e73\u884c\u6216\u5171\u7ebf,\u4e0d\u76f8\u4ea4 \r\n var denominator = nx1*ny2 - ny1*nx2; \r\n if (denominator==0) { \r\n return false; \r\n } \r\n \r\n \/\/\u5728\u6cd5\u7ebfN2\u4e0a\u7684\u6295\u5f71 \r\n var distC_N2=nx2 * c.x + ny2 * c.y; \r\n var distA_N2=nx2 * a.x + ny2 * a.y-distC_N2; \r\n var distB_N2=nx2 * b.x + ny2 * b.y-distC_N2; \r\n \r\n \/\/ \u70b9a\u6295\u5f71\u548c\u70b9b\u6295\u5f71\u5728\u70b9c\u6295\u5f71\u540c\u4fa7 (\u5bf9\u70b9\u5728\u7ebf\u6bb5\u4e0a\u7684\u60c5\u51b5,\u672c\u4f8b\u5f53\u4f5c\u4e0d\u76f8\u4ea4\u5904\u7406); \r\n if ( distA_N2*distB_N2&gt;=0 ) { \r\n return false; \r\n } \r\n \r\n \/\/ \r\n \/\/\u5224\u65ad\u70b9c\u70b9d \u548c\u7ebf\u6bb5ab\u7684\u5173\u7cfb, \u539f\u7406\u540c\u4e0a \r\n \/\/ \r\n \/\/\u5728\u6cd5\u7ebfN1\u4e0a\u7684\u6295\u5f71 \r\n var distA_N1=nx1 * a.x + ny1 * a.y; \r\n var distC_N1=nx1 * c.x + ny1 * c.y-distA_N1; \r\n var distD_N1=nx1 * d.x + ny1 * d.y-distA_N1; \r\n if ( distC_N1*distD_N1&gt;=0 ) { \r\n return false; \r\n } \r\n \r\n \/\/\u8ba1\u7b97\u4ea4\u70b9\u5750\u6807 \r\n var fraction= distA_N2 \/ denominator; \r\n var dx= fraction * ny1, \r\n dy= -fraction * nx1; \r\n return { x: a.x + dx , y: a.y + dy }; \r\n}\r\n\r\n<\/pre>\n<p>\u65b9\u6cd5\u4e09: \u5224\u65ad\u6bcf\u4e00\u6761\u7ebf\u6bb5\u7684\u4e24\u4e2a\u7aef\u70b9\u662f\u5426\u90fd\u5728\u53e6\u4e00\u6761\u7ebf\u6bb5\u7684\u4e24\u4fa7, \u662f\u5219\u6c42\u51fa\u4e24\u6761\u7ebf\u6bb5\u6240\u5728\u76f4\u7ebf\u7684\u4ea4\u70b9, \u5426\u5219\u4e0d\u76f8\u4ea4.<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\"> \r\nfunction segmentsIntr(a, b, c, d){ \r\n \r\n \/\/ \u4e09\u89d2\u5f62abc \u9762\u79ef\u76842\u500d \r\n var area_abc = (a.x - c.x) * (b.y - c.y) - (a.y - c.y) * (b.x - c.x); \r\n \r\n \/\/ \u4e09\u89d2\u5f62abd \u9762\u79ef\u76842\u500d \r\n var area_abd = (a.x - d.x) * (b.y - d.y) - (a.y - d.y) * (b.x - d.x); \r\n \r\n \/\/ \u9762\u79ef\u7b26\u53f7\u76f8\u540c\u5219\u4e24\u70b9\u5728\u7ebf\u6bb5\u540c\u4fa7,\u4e0d\u76f8\u4ea4 (\u5bf9\u70b9\u5728\u7ebf\u6bb5\u4e0a\u7684\u60c5\u51b5,\u672c\u4f8b\u5f53\u4f5c\u4e0d\u76f8\u4ea4\u5904\u7406); \r\n if ( area_abc*area_abd&gt;=0 ) { \r\n return false; \r\n } \r\n \r\n \/\/ \u4e09\u89d2\u5f62cda \u9762\u79ef\u76842\u500d \r\n var area_cda = (c.x - a.x) * (d.y - a.y) - (c.y - a.y) * (d.x - a.x); \r\n \/\/ \u4e09\u89d2\u5f62cdb \u9762\u79ef\u76842\u500d \r\n \/\/ \u6ce8\u610f: \u8fd9\u91cc\u6709\u4e00\u4e2a\u5c0f\u4f18\u5316.\u4e0d\u9700\u8981\u518d\u7528\u516c\u5f0f\u8ba1\u7b97\u9762\u79ef,\u800c\u662f\u901a\u8fc7\u5df2\u77e5\u7684\u4e09\u4e2a\u9762\u79ef\u52a0\u51cf\u5f97\u51fa. \r\n var area_cdb = area_cda + area_abc - area_abd ; \r\n if ( area_cda * area_cdb &gt;= 0 ) { \r\n return false; \r\n } \r\n \r\n \/\/\u8ba1\u7b97\u4ea4\u70b9\u5750\u6807 \r\n var t = area_cda \/ ( area_abd- area_abc ); \r\n var dx= t*(b.x - a.x), \r\n dy= t*(b.y - a.y); \r\n return { x: a.x + dx , y: a.y + dy }; \r\n \r\n}\r\n\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>\u65b9\u6cd5\u4e00: \u6c42\u4e24\u6761\u7ebf\u6bb5\u6240\u5728\u76f4\u7ebf\u7684\u4ea4\u70b9, \u518d\u5224\u65ad\u4ea4\u70b9\u662f\u5426\u5728\u4e24\u6761\u7ebf\u6bb5\u4e0a. \u65b9\u6cd5\u4e8c: \u5224\u65ad\u6bcf\u4e00\u6761\u7ebf\u6bb5\u7684\u4e24\u4e2a\u7aef\u70b9\u662f\u5426\u90fd\u5728<a href=\"https:\/\/www.ccagml.com\/?p=317\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">\u8be6\u89e3js\u5b9e\u73b0\u7ebf\u6bb5\u4ea4\u70b9\u7684\u4e09\u79cd\u7b97\u6cd5<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[30,20],"tags":[],"_links":{"self":[{"href":"https:\/\/www.ccagml.com\/index.php?rest_route=\/wp\/v2\/posts\/317"}],"collection":[{"href":"https:\/\/www.ccagml.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.ccagml.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.ccagml.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.ccagml.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=317"}],"version-history":[{"count":2,"href":"https:\/\/www.ccagml.com\/index.php?rest_route=\/wp\/v2\/posts\/317\/revisions"}],"predecessor-version":[{"id":319,"href":"https:\/\/www.ccagml.com\/index.php?rest_route=\/wp\/v2\/posts\/317\/revisions\/319"}],"wp:attachment":[{"href":"https:\/\/www.ccagml.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=317"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.ccagml.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=317"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.ccagml.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=317"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}