{"id":716,"date":"2009-11-16T21:48:30","date_gmt":"2009-11-16T13:48:30","guid":{"rendered":"http:\/\/test999.eblhost.com\/blog\/?p=716"},"modified":"2009-11-16T21:48:30","modified_gmt":"2009-11-16T13:48:30","slug":"processing%ef%bc%9a%e5%9b%be%e5%83%8f","status":"publish","type":"post","link":"https:\/\/learn.travelchinawith.me\/?p=716","title":{"rendered":"Processing\uff1a\u56fe\u50cf"},"content":{"rendered":"<p>1. \u8f7d\u5165\u4e00\u5f20\u56fe\u7247\uff1a<br \/>\n<dirtycode>\/\/ \u7533\u660e\u4e00\u4e2aPImage\u7c7b\u578b\u7684\u53d8\u91cf, \u4e00\u4e2a\u6211\u4eec\u53ef\u7528\u7684\u5b58\u50a8\u4e8ep5\u6838\u5fc3\u5e93\u5185\u7684\u7c7b.<br \/>\nPImage img; <\/p>\n<p>void setup() {<br \/>\n  size(320,240);<br \/>\n  \/\/ \u901a\u8fc7\u8f7d\u5165\u4e00\u4e2a\u56fe\u7247\u6587\u4ef6\u521b\u5efa\u4e00\u4e2a\u65b0\u7684PImage\u5b9e\u4f8b<br \/>\n  img = loadImage(&#8220;fuckyoubaby.jpg&#8221;);<br \/>\n}<\/p>\n<p>void draw() {<br \/>\n  background(0);<br \/>\n   \/\/image()\u51fd\u6570\u5728\u4e00\u4e2a\u4f4d\u7f6e\u663e\u793a\u56fe\u7247-\u672c\u4f8b\u4f4d\u4e8e\u70b9(0,0).<br \/>\n  image(img,0,0);<br \/>\n}<\/dirtycode><br \/>\n\u8fd9\u91cc\uff0c\u624b\u8d31\u7684\u6211\u53c8\u53d1\u73b0\u4e00\u70b9\uff1a\u5982\u679c\u4f60\u5c06\u4f60\u7684\u8fd9\u4e2asketch\u547d\u540d\u4e3aPImage\u7684\u8bdd\uff0c\u70b9\u51fb\u8fd0\u884cp5\u4f1a\u8fd4\u56de\u4f60\u9519\u8bef\uff1a&#8221;cannot convert from PImage to PImage&#8221;\u3002\u3002\u3002\u76f4\u5230\u4f60\u91cd\u547d\u540d\u540e\u624d\u80fd\u6b63\u5e38\u8fd0\u884c\u3002<\/p>\n<p>2. \u5728\u4e0a\u4f8b\u5185\u653e\u5165\u4e00\u5f20\u6bd4size\u5927\u7684\u56fe\u7247\uff0c\u8fd0\u884c\uff0c\u5b83\u5c06\u4ee5\u539f\u5c3a\u5bf8\u4ece\u5de6\u4e0a\u89d2\u5f00\u59cb\u663e\u793a\u56fe\u7247\uff0c\u4f60\u7684size\u6709\u591a\u5927\uff0c\u4ed6\u5c31\u663e\u793a\u591a\u5927\uff0c\u6ca1\u6709\u7f29\u653e\u3002<\/p>\n<p>3. \u8981\u663e\u793a\u7684\u56fe\u7247\u5fc5\u987b\u4f4d\u4e8e\u672csketch\u6587\u4ef6\u5939\u4e0b\u7684data\u6587\u4ef6\u5939\u5185\uff08\u8fd9\u4e5f\u662fp5\u8f7d\u5165\u5916\u90e8\u6587\u4ef6\u7684\u7edf\u4e00\u89c4\u77e9\uff09\u3002<\/p>\n<p>4. \u6211\u4eec\u9700\u8981\u6ce8\u610f\uff0c\u4ece\u786c\u76d8\u8f7d\u5165\u56fe\u7247\u76f8\u5bf9\u6765\u8bf4\u662f\u8f83\u6162\u7684\uff0c\u56e0\u6b64\u6211\u4eec\u5e94\u5f53\u786e\u4fdd\u6211\u4eec\u7684\u7a0b\u5e8f\u4ec5\u6267\u884c\u5176\u4e00\u6b21\uff0c\u56e0\u6b64\uff0c\u5e94\u5f53\u5c06\u5b83\u653e\u5230<em>setup()<\/em>\u800c\u4e0d\u662f<em>draw()<\/em>\u91cc\u3002<\/p>\n<p>5. \u4e00\u65e6\u56fe\u7247\u88ab\u8f7d\u5165\uff0c\u5b83\u5c06\u88ab<em>image()<\/em>\u51fd\u6570\u663e\u793a\u51fa\u6765\uff0c\u8fd9\u4e2a\u51fd\u6570\u5fc5\u5907\u4e09\u4e2a\u5f15\u6570\uff1a\u88ab\u663e\u793a\u7684\u56fe\u7247\u3001x\u4f4d\u7f6e\u548cy\u4f4d\u7f6e\uff0c\u53e6\u5916\uff0c\u540e\u8fb9\u8fd8\u53ef\u63a5\u4e24\u4e2a\u5907\u9009\u53c2\u6570\uff1a\u56fe\u50cf\u7684\u5bbd\u548c\u9ad8\uff08\u5168\u56fe\u7f29\u653e\uff09\u3002<\/p>\n<p>6. \u8ba9\u56fe\u7247\u52a8\u8d77\u6765\uff1a<br \/>\n<dirtycode>PImage dick; \/\/ \u4e3a\u56fe\u7247\u6587\u4ef6\u51c6\u5907\u7684\u53d8\u91cf<br \/>\nfloat x,y;   \/\/ \u56fe\u7247\u4f4d\u7f6e\u53d8\u91cf<br \/>\nfloat rot;   \/\/ \u56fe\u7247\u65cb\u8f6c\u53d8\u91cf<\/p>\n<p>void setup() {<br \/>\n  size(200,200);<\/p>\n<p>  \/\/ \u8f7d\u5165\u56fe\u50cf, \u521d\u59cb\u5316\u53d8\u91cf<br \/>\n  dick = loadImage(&#8220;fuckyoubaby.jpg&#8221;);<br \/>\n  x = 0.0;<br \/>\n  y = width\/2.0;<br \/>\n  rot = 0.0;<br \/>\n}<\/p>\n<p>void draw() {<br \/>\n  background(255);<\/p>\n<p>  \/\/ \u4f4d\u79fb\u548c\u5f62\u53d8<br \/>\n  translate(x,y);<br \/>\n  rotate(rot);<\/p>\n<p>  \/\/ \u56fe\u7247\u53ef\u4ee5\u50cf\u5176\u4ed6\u5e38\u89c4\u5f62\u72b6\u4e00\u6837\u4f7f\u7528\u53d8\u91cf\u3001translate(), rotate()\u7b49\u7b49\u8fdb\u884c\u52a8\u753b\u5236\u4f5c.<br \/>\n  image(dick,0,0); <\/p>\n<p>  \/\/ \u8c03\u6574\u52a8\u753b\u53d8\u91cf<br \/>\n  x += 1.0;<br \/>\n  rot += 0.02;<br \/>\n  if (x > width+head.width) {<br \/>\n    x = -head.width;<br \/>\n  }<br \/>\n}<\/dirtycode><\/p>\n<p>7. \u5c0f\u4e39\u51fa\u9898\uff1a\u7528\u7c7b\u91cd\u5199\u4e0a\u4f8b\uff0c\u662f\u7684\uff0c\u548c\u4f60\u4e00\u6837\uff0c\u76ee\u524d\u4e3a\u6b62\u6211\u5bf9\u7c7b\u7684\u4f7f\u7528\u8fd8\u4e0d\u662f\u90a3\u4e48\u8212\u670d\uff0cwell\uff0c\u4e00\u8d77\u770b<a href=\"http:\/\/www.learningprocessing.com\/exercises\/chapter-15\/exercise-15-2\">\u7b54\u6848<\/a>\u5427\uff0c\u6211\u59cb\u7ec8\u575a\u4fe1\u770b\u7740\u770b\u7740\u770b\u591a\u4e86\u4e5f\u5c31\u4f1a\u4e86\u3002\u3002<\/p>\n<p>8. \u5982\u679c\u4f60\u8981\u6539\u53d8\u56fe\u7247\u663e\u793a\u7684\u6837\u5b50\uff0c<em>tint()<\/em>\u53ef\u4ee5\u5e2e\u4f60\uff0c<em>tint()<\/em>\u4e4b\u4e8e\u56fe\u7247\u5c31\u50cf<em>fill()<\/em>\u4e4b\u4e8e\u5f62\u72b6\u3002tint()\u7684\u5f15\u6570\u53ef\u4ee5\u7b80\u5355\u6307\u5b9a\u6bcf\u4e2a\u50cf\u7d20\u53ef\u4ee5\u4f7f\u7528\u7684\u989c\u8272\u6570\u91cf\u4ee5\u53ca\u900f\u660e\u5ea6\u3002<\/p>\n<figure id=\"attachment_717\" aria-describedby=\"caption-attachment-717\" style=\"width: 320px\" class=\"wp-caption alignright\"><a href=\"https:\/\/learn.travelchinawith.me\/wp-content\/uploads\/2009\/11\/pr15-1.jpg\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/learn.travelchinawith.me\/wp-content\/uploads\/2009\/11\/pr15-1.jpg\" alt=\"tint()\" title=\"pr15-1\" width=\"320\" height=\"231\" class=\"size-full wp-image-717\" srcset=\"https:\/\/learn.travelchinawith.me\/wp-content\/uploads\/2009\/11\/pr15-1.jpg 320w, https:\/\/learn.travelchinawith.me\/wp-content\/uploads\/2009\/11\/pr15-1-300x216.jpg 300w\" sizes=\"auto, (max-width: 320px) 100vw, 320px\" \/><\/a><figcaption id=\"caption-attachment-717\" class=\"wp-caption-text\">tint()<\/figcaption><\/figure>\n<p>\u25ce \u5982\u679ctint()\u53ea\u63a5\u6536\u5230\u4e00\u4e2a\u5f15\u6570\uff0c\u53ea\u6709\u56fe\u50cf\u7684\u4eae\u5ea6\u4f1a\u53d7\u5230\u5f71\u54cd\u3002<br \/>\n<dirtycode:noclick>\/\/\u539f\u59cb\u4eae\u5ea6<br \/>\ntint(255);<br \/>\nimage(sunflower,0,0);<\/dirtycode><br \/>\n<dirtycode:noclick>\/\/\u53d8\u6697<br \/>\ntint(100);<br \/>\nimage(sunflower,0,0);<\/dirtycode><br \/>\n\u25ce \u7b2c\u4e8c\u4e2a\u5f15\u6570\u5c06\u6539\u53d8\u56fe\u7247\u7684alpha\u900f\u660e\u5ea6<br \/>\n<dirtycode:noclick>\/\/\u8bbe\u7f6e\u56fe\u7247\u4e0d\u900f\u660e\u5ea6\u4e3a50%<br \/>\ntint(255,127);<br \/>\nimage(sunflower,0,0); <\/dirtycode><br \/>\n\u25ce \u4e09\u4e2a\u5f15\u6570\u5f71\u54cd\u7ea2\u3001\u7eff\u3001\u84dd\u4e09\u8272\u7ec4\u6210\u7684\u4eae\u5ea6\u3002<br \/>\n<dirtycode:noclick>\/\/\u6ca1\u6709\u7ea2\u8272\uff0c\u5927\u90e8\u5206\u4e3a\u7eff\u8272\uff0c\u5168\u90e8\u4e3a\u84dd\u8272<br \/>\ntint(0,200,255)<br \/>\nimage(sunflower,0,0);<\/dirtycode><br \/>\n\u25ce \u6700\u540e\uff0c\u6dfb\u52a0\u7b2c\u56db\u4e2a\u5f15\u6570\u540c\u6837\u63a7\u5236alpha\u900f\u660e\u5ea6\u3002tint()\u503c\u7684\u8303\u56f4\u53ef\u7528colorMode()\u6307\u5b9a\u3002<br \/>\n<dirtycode:noclick>\/\/\u8bbe\u7f6e\u56fe\u7247\u4e3a\u7ea2\u8272\u8c03\u5e76\u900f\u660e<br \/>\ntint(255,0,0,100);<br \/>\nimage(sunflower,0,0);<\/dirtycode><\/p>\n<p>9. \u4e00\u5f20\u56fe\u7247\u4e0d\u723d\uff0c\u6211\u4eec\u8981\u4e00\u5806\u56fe\u7247\uff01\u6211\u4eec\u4ece\u8fd9\u4e2a\u70b9\u5b50\u5f00\u59cb\u641e\uff0c\u8f7d\u5165\u4e94\u5f20\u56fe\u7247\uff0c\u6bcf\u70b9\u51fb\u9f20\u6807\u4e00\u6b21\u66f4\u6362\u4e00\u5f20\u3002<br \/>\n\u9996\u5148\uff0c\u6211\u4eec\u5efa\u7acb\u4e00\u4e2a\u56fe\u7247\u7684\u6570\u7ec4\u4f5c\u4e3a\u5168\u5c40\u53d8\u91cf\uff1a<br \/>\n<dirtycode:noclick>PImage[] images = new PImage[5];<\/dirtycode><br \/>\n\u7b2c\u4e8c\uff0c\u6211\u4eec\u5728setup()\u5185\u5c06\u6bcf\u5f20\u56fe\u7247\u585e\u5165\u6570\u7ec4\u5185\u6b63\u786e\u7684\u4f4d\u7f6e\uff1a<br \/>\n<dirtycode:noclick>images[0] = loadImage( &#8221; cat.jpg &#8221; );<br \/>\nimages[1] = loadImage( &#8221; mouse.jpg &#8221; );<br \/>\nimages[2] = loadImage( &#8221; dog.jpg &#8221; );<br \/>\nimages[3] = loadImage( &#8221; kangaroo.jpg &#8221; );<br \/>\nimages[4] = loadImage( &#8221; porcupine.jpg &#8221; );<\/dirtycode><br \/>\n\u662f\u7684\uff0c\u5355\u72ec\u624b\u52a8\u8f7d\u5165\u4e00\u5f20\u5f20\u56fe\u7247\u7684\u505a\u6cd5\u663e\u5f97\u5e76\u4e0d\u5341\u5206\u4f18\u96c5\uff0c\u800c\u4e14\u5047\u8bbe\u6211\u4eec\u8981\u8f7d\u5165100\u5f20\u56fe\u7247\u4f60\u8fd8\u4e0d\u5f97\u50bb\u903c\u4e86\u554a\uff1f\u4e00\u4e2a\u89e3\u51b3\u529e\u6cd5\u662f\u5c06\u6587\u4ef6\u540d\u50a8\u5b58\u4e8e\u4e00\u4e2a\u5b57\u7b26\u4e32\u5185\u7136\u540e\u4f7f\u7528for\u5faa\u73af\u521d\u59cb\u5316\u5168\u90e8\u6570\u7ec4\u5143\u7d20\uff1a<br \/>\n<dirtycode:noclick>String [] \ufb01lenames = {&#8220;cat.jpg&#8221; , &#8220;mouse.jpg&#8221;, &#8220;dog.jpg&#8221; , &#8220;kangaroo.jpg&#8221; , &#8220;porcupine.jpg&#8221; );<br \/>\nfor (int i = 0; i < \ufb01lenames.length; i++)  {    \n    images[i] = loadImage(\ufb01lenames[i]);   \n}<\/dirtycode><\/p>\n<pre>\u77e5\u8bc6\u4e32\u8054\uff1a\u5728\u5b57\u7b26\u4e32\u5185\uff1a\u201cHappy\u201d \uff0b \u201c Trails\u201d \uff1d \u201cHappy Trails\u201d\uff0c\u201c2\u201d \uff0b \u201c2\u201d \uff1d \u201c22\u201d\u3002<\/pre>\n<p>\u66f4\u68d2\u7684\u662f\uff1a\u5982\u679c\u6211\u4eec\u5c06\u56fe\u7247\u6309\u987a\u5e8f\u547d\u540d ( \u201canimal1.jpg\u201d ,  \u201canimal2.jpg\u201d ,  \u201canimal3.jpg\u201d , \u7b49\u7b49)\uff0c\u6211\u4eec\u7684\u4ee3\u7801\u957f\u5ea6\u5c06\u5f97\u5230\u5927\u5927\u51cf\u77ed\uff1a<br \/>\n<dirtycode:noclick>for (int i = 0; i < images.length; i++) {   \n  images[i] = loadImage( \"animal\" + i + \".jpg\"); \n}<\/dirtycode><br \/>\n\u5f53\u56fe\u7247\u8f7d\u5165\u5b8c\u6bd5\uff0c\u6211\u4eec\u5c06\u8fdb\u5165draw()\u663e\u793a\u5b83\uff1a<br \/>\n<dirtycode:noclick>image(images[0],0,0);<\/dirtycode><br \/>\n\u5f53\u7136\uff0c\u6309\u987a\u5e8f\u4e00\u4e2a\u4e2a\u628a\u56fe\u7247\u987a\u5e8f\u7684\u503c\u5199\u51fa\u6765\u7684\u662f\u50bb\u903c\u3002\u6211\u4eec\u9700\u8981\u4e00\u4e2a\u53ef\u4ee5\u52a8\u6001\u53d8\u52a8\u7684\u53d8\u91cf\u6765\u4ee3\u66ff\u5b83\uff0c\u4ee5\u5e94\u5bf9\u4e0d\u540c\u65f6\u523b\u70b9\u51fb\u7684\u56fe\u7247\u663e\u793a\uff1a<br \/>\n<dirtycode:noclick>image(images[imageindex],0,0);<\/dirtycode><br \/>\n\u5168\u90e8\u4ee3\u7801\uff1a<br \/>\n<dirtycode>int maxImages = 10; \/\/ \u56fe\u7247\u7684\u603b\u6570<br \/>\nint imageIndex = 0; \/\/ \u6700\u521d\u7684\u56fe\u7247\u6700\u5148\u663e\u793a<\/p>\n<p>\/\/ \u7533\u660e\u4e00\u4e2a\u56fe\u7247\u6570\u7ec4.<br \/>\nPImage[] images = new PImage[maxImages]; <\/p>\n<p>void setup() {<br \/>\n  size(200,200);<\/p>\n<p>  \/\/ \u5c06\u56fe\u7247\u8f7d\u5165\u6570\u7ec4<br \/>\n  \/\/ \u522b\u5fd8\u4e86\u5c06\u56fe\u7247\u653e\u5165data\u6587\u4ef6\u5939!<br \/>\n  for (int i = 0; i < images.length; i ++ ) {\n    images[i] = loadImage( \"animal\" + i + \".jpg\" ); \n  }\n}\n\nvoid draw() {\n  \/\/ \u663e\u793a\u4e00\u526f\u56fe\n  image(images[imageIndex],0,0); \n}\n\nvoid mousePressed() {\n  \/\/ \u5f53\u9f20\u6807\u70b9\u51fb\u540e\u968f\u673a\u9009\u53d6\u4e00\u526f\u56fe\u7247\u663e\u793a\n  \/\/ \u6ce8\u610f\u6570\u7ec4\u7684\u7d22\u5f15\u5fc5\u987b\u662f\u6574\u6570!\n  imageIndex = int(random(images.length));\n}<\/dirtycode><\/p>\n<p>10. \u987a\u5e8f\u52a8\u6001\u64ad\u653e\u4e00\u7ec4\u56fe\u7247\uff1a<br \/>\n<dirtycode>int maxImages = 10; \/\/ \u56fe\u7247\u7684\u603b\u6570<br \/>\nint imageIndex = 0; \/\/ \u6700\u521d\u7684\u56fe\u7247\u6700\u5148\u663e\u793a<\/p>\n<p>\/\/ \u7533\u660e\u4e00\u4e2a\u56fe\u7247\u6570\u7ec4.<br \/>\nPImage[] images = new PImage[maxImages]; <\/p>\n<p>void setup() {<br \/>\n  size(200,200);<\/p>\n<p>  \/\/ \u5c06\u56fe\u7247\u8f7d\u5165\u6570\u7ec4<br \/>\n  \/\/ \u522b\u5fd8\u4e86\u5c06\u56fe\u7247\u653e\u5165data\u6587\u4ef6\u5939!<br \/>\n  for (int i = 0; i < images.length; i ++ ) {\n    images[i] = loadImage( \"animal\" + i + \".jpg\" ); \n  }\n  frameRate(5);\n}\n\nvoid draw() {\n  \n  background(0);\n  image(images[imageIndex],0,0);\n  \n  \/\/ \u6bcf\u6b21\u5faa\u73af\u4e3a\u7d22\u5f15\u52a0\u4e00\n  \/\/ \u4e00\u65e6\u5230\u8fbe\u6570\u7ec4\u672b\u5c3e\uff0c\u4f7f\u7528\u6a21 \" % \"\u8fd4\u56de0\n  imageIndex = (imageIndex + 1) % images.length;\n}<\/dirtycode><\/p>\n<p>11. \u6211\u4eec\u77e5\u9053\u6211\u4eec\u7684\u8424\u5e55\u662f\u7531\u50cf\u7d20\u7ec4\u6210\u7684\uff0c\u6211\u4eec\u4e4b\u6240\u4ee5\u80fd\u7528<em>line()<\/em>\u7ed8\u5236\u4e24\u70b9\u95f4\u7684\u76f4\u7ebf\uff0c\u662f\u56e0\u4e3ap5\u4e3a\u6211\u4eec\u81ea\u52a8\u586b\u6ee1\u4e86\u8fd9\u4e24\u70b9\u95f4\u6240\u6709\u50cf\u7d20\u7684\u70b9\u3002\u50cf\u7d20\u6570\u7ec4\u53ea\u6709\u4e00\u7ef4\uff0c\u4ece\u5de6\u4e4b\u53f3\u3001\u4ece\u4e0a\u5f80\u4e0b\u6570\u5e8f\u6392\u5217\u3002<\/p>\n<figure id=\"attachment_718\" aria-describedby=\"caption-attachment-718\" style=\"width: 332px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/learn.travelchinawith.me\/wp-content\/uploads\/2009\/11\/pr15-2.gif\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/learn.travelchinawith.me\/wp-content\/uploads\/2009\/11\/pr15-2.gif\" alt=\"\u50cf\u7d20\u5e03\u5c40\" title=\"pr15-2\" width=\"332\" height=\"192\" class=\"size-full wp-image-718\" srcset=\"https:\/\/learn.travelchinawith.me\/wp-content\/uploads\/2009\/11\/pr15-2.gif 332w, https:\/\/learn.travelchinawith.me\/wp-content\/uploads\/2009\/11\/pr15-2-300x173.gif 300w\" sizes=\"auto, (max-width: 332px) 100vw, 332px\" \/><\/a><figcaption id=\"caption-attachment-718\" class=\"wp-caption-text\">\u50cf\u7d20\u5e03\u5c40<\/figcaption><\/figure>\n<p>\u4e0b\u4f8b\u5728\u6bcf\u4e2a\u50cf\u7d20\u4e0a\u586b\u4e0a\u4e86\u968f\u673a\u7070\u8272\u503c\u3002pixles\u6570\u7ec4\u548c\u5176\u4ed6\u6570\u7ec4\u4e00\u6837\uff0c\u552f\u4e00\u4e0d\u540c\u5373\u6211\u4eec\u65e0\u9700\u7533\u660e\u5b83\uff0c\u56e0\u4e3a\u5b83\u662fp5\u7684\u5185\u7f6e\u53d8\u91cf\uff1a<br \/>\n<dirtycode>size(200,200);<\/p>\n<p>\/\/ \u5728\u6211\u4eec\u5904\u7406\u50cf\u7d20\u524d<br \/>\nloadPixels();<\/p>\n<p>\/\/ \u5728\u6bcf\u4e2a\u50cf\u7d20\u95f4\u5faa\u73af<br \/>\nfor (int i = 0; i < pixels.length; i++ ) { \/\/ \u6211\u4eec\u53ef\u4ee5\u50cf\u5904\u7406\u5176\u4ed6\u6570\u7ec4\u90a3\u6837\u5f97\u5230\u50cf\u7d20\u6570\u7ec4\u7684\u957f\u5ea6\n  \n  \/\/ \u9009\u53d6\u4e00\u4e2a\u968f\u673a\u6570, 0\u5230255\n  float rand = random(255);\n  \n  \/\/ \u57fa\u4e8e\u968f\u673a\u6570\u521b\u5efa\u4e00\u4e2a\u7070\u8272\n  color c = color(rand);\n  \n  \/\/ \u4e3a\u6bcf\u4e2a\u50cf\u7d20\u8bbe\u7f6e\u968f\u673a\u8272\n  pixels[i] = c; \/\/ \u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u7d22\u5f15\u8fdb\u5165\u50cf\u7d20\u6570\u7ec4\u7684\u5355\u72ec\u5143\u7d20\uff0c\u4e0e\u5904\u7406\u5176\u4ed6\u6570\u7ec4\u5e76\u65e0\u4e8c\u81f4.\n}\n\n\/\/ \u5f53\u6211\u4eec\u5b8c\u6210\u50cf\u7d20\u5904\u7406\u5de5\u4f5c\nupdatePixels();<\/dirtycode><\/p>\n<p>12. \u5982\u4e0a\u4f8b\uff0c\u6211\u4eec\u65e0\u9700\u62c5\u5fc3\u50cf\u7d20\u7684\u5177\u4f53\u4f4d\u7f6e\uff0c\u4f46\u662f\u76f8\u7d20\u7684XY\u4f4d\u7f6e\u5728\u5f88\u591a\u5236\u56fe\u8f6f\u4ef6\u4e2d\u662f\u81f3\u5173\u91cd\u8981\u7684\u4fe1\u606f\u3002\u4e00\u4e2a\u7b80\u5355\u7684\u4f8b\u5b50\uff1a\u8bbe\u7f6e\u6bcf\u4e2a\u5076\u6570\u5217\u50cf\u7d20\u4e3a\u767d\u3001\u6bcf\u4e2a\u5947\u6570\u5217\u50cf\u7d20\u4e3a\u9ed1\u3002\u901a\u8fc7\u4e00\u4e2a\u4e00\u7ef4\u50cf\u7d20\u6570\u7ec4\u4f60\u5982\u4f55\u505a\u5230\u8fd9\u4e00\u70b9\uff1f\u4f60\u5982\u4f55\u77e5\u9053\u4efb\u610f\u7ed9\u5b9a\u7684\u50cf\u7d20\u4f4d\u4e8e\u7b2c\u51e0\u884c\u7b2c\u51e0\u5217\uff1f\u5f53\u4f7f\u7528\u50cf\u7d20\u7f16\u7a0b\u7684\u65f6\u5019\uff0c\u6211\u4eec\u9700\u8981\u8bbe\u60f3\u6bcf\u4e2a\u50cf\u7d20\u90fd\u5b58\u5728\u4e8e\u4e8c\u7ef4\u4e16\u754c\u4e2d\uff0c\u4f46\u662f\u6301\u7eed\u5728\u4e00\u7ef4\u63a5\u6536\u6570\u636e\u3002\u6211\u4eec\u53ef\u4ee5\u7528\u5982\u4e0b\u516c\u5f0f\uff1a<br \/>\n1\uff09\u5047\u8bbe\u4e00\u4e2a\u7a97\u53e3\u6216\u56fe\u50cf\u5927\u5c0f\u4e3a\u7ed9\u5b9a\u7684\u5bbd\u5ea6\u548c\u9ad8\u5ea6\u3002<br \/>\n2\uff09\u4e8e\u662f\u6211\u4eec\u77e5\u9053\u4e86\u50cf\u7d20\u603b\u6570\u4e3a\u5bbd*\u9ad8\u3002<br \/>\n3\uff09\u5bf9\u4e8e\u4efb\u4f55\u7ed9\u5b9a\u7684X\u3001Y\u70b9\uff0c\u5b83\u5728\u6211\u4eec\u7684\u4e00\u7ef4\u50cf\u7d20\u6570\u7ec4\u4e2d\u7684\u4f4d\u7f6e\u662f\uff1a<strong>\u4f4d\u7f6e\uff1dX\uff0bY*\u5bbd\u5ea6<\/strong><\/p>\n<figure id=\"attachment_719\" aria-describedby=\"caption-attachment-719\" style=\"width: 399px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/learn.travelchinawith.me\/wp-content\/uploads\/2009\/11\/pr15-3.gif\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/learn.travelchinawith.me\/wp-content\/uploads\/2009\/11\/pr15-3.gif\" alt=\"\u50cf\u7d20\u4f4d\u7f6e\u53ca\u5176x\u3001y\u4f4d\u7f6e\u5173\u7cfb\" title=\"pr15-3\" width=\"399\" height=\"191\" class=\"size-full wp-image-719\" srcset=\"https:\/\/learn.travelchinawith.me\/wp-content\/uploads\/2009\/11\/pr15-3.gif 399w, https:\/\/learn.travelchinawith.me\/wp-content\/uploads\/2009\/11\/pr15-3-300x143.gif 300w\" sizes=\"auto, (max-width: 399px) 100vw, 399px\" \/><\/a><figcaption id=\"caption-attachment-719\" class=\"wp-caption-text\">\u50cf\u7d20\u4f4d\u7f6e\u53ca\u5176x\u3001y\u4f4d\u7f6e\u5173\u7cfb<\/figcaption><\/figure>\n<p>13. \u597d\u7684\uff0c\u73b0\u5728\u8ba9\u6211\u4eec\u6765\u5904\u7406\u4e0a\u8fb9\u90a3\u4e2a\u5947\u6570\u548c\u5076\u6570\u5217\u7684\u95ee\u9898\uff1a<br \/>\n<dirtycode>size(200,200);<br \/>\nloadPixels();<\/p>\n<p> \/\/ \u4e24\u4e2a\u5faa\u73af\u5141\u8bb8\u6211\u4eec\u8bbf\u95ee\u5230\u6bcf\u4e2a\u5217 (x) \u548c\u884c (y).<\/p>\n<p>\/\/ \u5728\u6bcf\u4e2a\u50cf\u7d20\u5217\u95f4\u5faa\u73af<br \/>\nfor (int x = 0; x < width; x++ ) {\n  \/\/ \u5728\u6bcf\u4e2a\u50cf\u7d20\u884c\u95f4\u5faa\u73af\n  for (int y = 0; y < height; y++ ) {\n    \n    \/\/ \u4f7f\u7528\u516c\u5f0f\u627e\u5230\u4e00\u7ef4\u4f4d\u7f6e\n    int loc = x + y * width;\n    \n    if (x % 2 == 0) { \/\/ \u5982\u679c\u6211\u4eec\u4f4d\u4e8e\u5076\u6570\u5217\n      pixels[loc] = color(255);\n    } else { \/\/ \u5982\u679c\u6211\u4eec\u4f4d\u4e8e\u5947\u6570\u5217\n      pixels[loc] = color(0); \/\/   \u6211\u4eec\u4f7f\u7528\u5217\u6570\u5b57 (x) \u6765\u51b3\u5b9a\u989c\u8272\u662f\u9ed1\u662f\u767d\n    }\n  }\n}\n\nupdatePixels();<\/dirtycode><\/p>\n<p>14. \u8fd9\u4e2a\u7ec3\u4e60\u7684\u6548\u679c\u5f88\u6709\u610f\u601d\uff1a<br \/>\n<dirtycode>size(255, 255);<br \/>\nloadPixels();<br \/>\nfor (int x = 0; x < width; x++) {\n  for (int y = 0; y < height; y++) {\n    int loc = x+y*width;\n    float distance =dist(x,y,width\/2,height\/2);\n    pixels[loc] = color(distance);\t\n  }\n}\nupdatePixels();<\/dirtycode><\/p>\n<p>15. \u8fd8\u6709\u8fd9\u4e2a\uff1a<br \/>\n<dirtycode>size(255, 255);<br \/>\nloadPixels();<br \/>\nfor (int x = 0; x < width; x++) {\n  for (int y = 0; y < height; y++) {\n    int loc = x + y*width;\n    if (x < width\/2) {\n      pixels[loc] = color(x);\n    } else {\n     pixels[loc] = color(y);\n    }\n  }\n}\nupdatePixels();<\/dirtycode><br \/>\n\u662f\u7684\uff0c\u8fd9\u4e2a\u6709\u70b9\u96be\u4ee5\u7406\u89e3\uff0c\u4e0d\u8fc7\u8fd8\u662f\u7ee7\u7eed\u5411\u4e0b\u5427\uff0c\u73b0\u5728\u60f3\u4e0d\u901a\u4e0d\u7b49\u4e8e\u4ee5\u540e\u60f3\u4e0d\u901a\u3002\u3002<\/p>\n<p>16. \u4e4b\u524d\u7684\u56fe\u50cf\u4f8b\u5b50\u90fd\u662f\u57fa\u4e8e\u4efb\u610f\u8ba1\u7b97\u7684\u50cf\u7d20\u503c\u3002\u73b0\u5728\u6211\u4eec\u5c06\u5b66\u4e60\u5982\u4f55\u901a\u8fc7\u4ece\u4e00\u4e2a\u5df2\u77e5PImage\u5bf9\u8c61\u4e2d\u627e\u5230\u7684\u6765\u8bbe\u7f6e\u50cf\u7d20\uff0c\u4e0b\u8fb9\u662f\u4e00\u4e9b\u4f2a\u7801\uff1a<br \/>\n1\uff09\u5c06\u4e00\u4e2a\u56fe\u50cf\u8f7d\u5165PImage\u5bf9\u8c61\u3002<br \/>\n2\uff09\u5bf9\u4e8ePImage\u4e2d\u7684\u6bcf\u4e2a\u50cf\u7d20\uff0c\u68c0\u7d22\u5176\u989c\u8272\u5e76\u7528\u90a3\u4e2a\u989c\u8272\u989c\u8272\u50cf\u7d20\u3002<br \/>\nPImage\u5305\u542b\u5f88\u591a\u6709\u7528\u7684\u50a8\u5b58\u56fe\u50cf\u6570\u636e\u7684\u57df\uff08field\uff09\u2014\u2014\u5bbd\u5ea6\u3001\u9ad8\u5ea6\u548c\u50cf\u7d20\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528\u70b9\u8bed\u6cd5\uff08dot syntax\uff09\u8fdb\u5165\u8fd9\u4e9b\u57df\uff1a<br \/>\n<dirtycode:noclick>PImage img = createImage(320,240,RGB); \/\/ \u521b\u5efa\u4e00\u4e2a PImage\u5bf9\u8c61<br \/>\nprintln(img.width); \/\/ Yields 320<br \/>\nprintln(img.height); \/\/ Yields 240<br \/>\nimg.pixels[0] = color(255,0,0); \/\/ \u8bbe\u7f6e\u7b2c\u4e00\u4e2a\u50cf\u7d20\u989c\u8272\u4e3a\u7ea2\u8272<\/dirtycode><\/p>\n<p>17. \u8fdb\u5165\u8fd9\u4e9b\u57df\u5141\u8bb8\u6211\u4eec\u5faa\u73af\u4e00\u4e2a\u56fe\u50cf\u7684\u5168\u90e8\u50cf\u7d20\u5e76\u5c06\u5b83\u4eec\u663e\u793a\u5728\u8424\u5e55\u4e0a\u3002<br \/>\n<dirtycode>PImage img;<\/p>\n<p>void setup() {<br \/>\n  size(320 ,214);<br \/>\n  img = loadImage(&#8220;fuckyoubaby.jpg&#8221;);<br \/>\n}<\/p>\n<p>void draw() {<br \/>\n  loadPixels();<\/p>\n<p>  \/\/ \u7531\u4e8e\u8981\u8bfb\u53d6\u5b83\u7684\u50cf\u7d20\uff0c\u6211\u4eec\u5fc5\u987b\u540c\u6837\u5728PImage\u547c\u53ebloadPixels()<br \/>\n  img.loadPixels();<br \/>\n  for (int y = 0; y < height; y++ ) {\n    for (int x = 0; x < width; x++ ) {\n      int loc = x + y*width;\n      \/\/ \u51fd\u6570red(), green(), and blue()\u5c06\u4e09\u4e2a\u989c\u8272\u7684\u6df7\u5408\u8272\u663e\u793a\u5728\u5404\u4e2a\u50cf\u7d20\u4e0a.\n      float r = red(img.pixels [loc]); \n      float g = green(img.pixels[loc]);\n      float b = blue(img.pixels[loc]);\n\n      \/\/ \u989c\u8272\u5904\u7406\u5c06\u524d\u5f80\u8fd9\u91cc\n      \/\/ \u5982\u679c\u6211\u4eec\u60f3\u66f4\u6539RGB\u503c, \u6211\u4eec\u53ef\u4ee5\u5728\u8fd9\u91cc\u505a\uff08\u5728\u8bbe\u7f6e\u50cf\u7d20\u663e\u793a\u4e8e\u8424\u5e55\u524d\uff09.\n\n      \/\/ \u8bbe\u7f6e\u663e\u793a\u50cf\u7d20\u5230\u56fe\u7247\u50cf\u7d20\u4e0a\n      pixels[loc] = color(r,g,b);\n    }\n  }\n  \n  updatePixels();\n}<\/dirtycode><br \/>\n\u5f3a\u8c03\u4e00\u70b9\uff0c\u5728\u4e0a\u4f8b\u4e2d\uff0c\u56e0\u4e3a\u663e\u793a\u533a\u57df\u4e0e\u539f\u56fe\u5927\u5c0f\u76f8\u540c\uff0c\u6240\u4ee5\u8fd9\u4e48\u5199\u6ca1\u95ee\u9898\u3002\u4f46\u662f\u5982\u679c\u4e8c\u8005\u5927\u5c0f\u4e0d\u540c\u7684\u8bdd\uff0c\u4f60\u9700\u8981\u8fdb\u884c\u4e24\u4e2a\u50cf\u7d20\u4f4d\u7f6e\u7684\u8ba1\u7b97\uff0c\u4e00\u4e2a\u4e3a\u539f\u56fe\uff0c\u4e00\u4e2a\u4f2a\u663e\u793a\u533a<br \/>\n<dirtycode:noclick>int imageLoc = x + y*img.width;<br \/>\nint displayLoc = x + y*width;<\/dirtycode><\/p>\n<p>18. \u662f\u7684\uff0c\u5982\u679c\u6211\u4eec\u53ef\u4ee5\u63a7\u5236\u6bcf\u4e2a\u50cf\u7d20\uff0c\u4e5f\u5c31\u610f\u5473\u7740\u6211\u4eec\u53ef\u4ee5\u57fa\u4e8e\u6b64\u73a9\u51fa\u66f4\u591a\u7684\u628a\u620f\uff0c\u8bf7\u770b\u4e0b\u4f8b\uff0c\u9f20\u6807\u6c34\u5e73\u4f4d\u7f6e\u63a7\u5236\u56fe\u50cf\u4eae\u5ea6:<br \/>\n<dirtycode>PImage img;<\/p>\n<p>void setup() {<br \/>\n  size(320,214);<br \/>\n  img = loadImage(&#8220;fuckyoubaby.jpg&#8221;);<br \/>\n}<\/p>\n<p>void draw() {<br \/>\n  loadPixels();<\/p>\n<p> img.loadPixels();<br \/>\n  for (int x = 0; x < img.width; x++ ) {\n    for (int y = 0; y < img.height; y++ ) {\n\n      \/\/ \u8ba1\u7b971D\u50cf\u7d20\u4f4d\u7f6e\n      int loc = x + y*img.width;\n\n      \/\/ \u83b7\u53d6\u56fe\u50cfR,G,B\u503c\n      float r = red (img.pixels[loc]);\n      float g = green (img.pixels[loc]);\n      float b = blue (img.pixels[loc]);\n\n      \/\/ \u7528\u9f20\u6807\u4f4d\u7f6e\u548c\u4e58\u6cd5\u8ba1\u7b97\u4e00\u4e2a0.0\u52308.0\u7684\u8303\u56f4 \n      \/\/ \u8fd9\u4e2a\u4e58\u6cd5\u6539\u53d8\u4e86\u6bcf\u4e2a\u50cf\u7d20\u7684RGB\u503c.      \n      float adjustBrightness = ((float) mouseX \/ width) * 8.0; \n      r *= adjustBrightness;\n      g *= adjustBrightness;\n      b *= adjustBrightness;\n\n      \/\/ \u5728\u8bbe\u7f6e\u4e3a\u65b0\u989c\u8272\u524d\uff0cRGB\u503c\u88ab\u9650\u5236\u57280\u5230255\u95f4.      \n      r = constrain(r,0,255); \n      g = constrain(g,0,255);\n      b = constrain(b,0,255);\n\n      \/\/ \u5236\u4f5c\u4e00\u4e2a\u65b0\u989c\u8272\u5e76\u5728\u7a97\u53e3\u8bbe\u7f6e\u50cf\u7d20\n      color c = color(r,g,b);\n      pixels[loc] = c;\n    }\n  }\n  \n  updatePixels();  \n}<\/dirtycode><\/p>\n<p>19. \u7531\u4e8e\u6211\u4eec\u5728\u6bcf\u4e2a\u50cf\u7d20\u7684\u57fa\u7840\u4e0a\u6539\u53d8\u56fe\u50cf\uff0c\u56e0\u6b64\u4e0d\u9700\u8981\u5e73\u7b49\u5bf9\u5f85\u6bcf\u4e00\u50cf\u7d20\u3002\u4f8b\u5982\uff0c\u6211\u4eec\u53ef\u4ee5\u6839\u636e\u5176\u8ddd\u79bb\u9f20\u6807\u7684\u8ddd\u79bb\u6539\u53d8\u6bcf\u4e2a\u50cf\u7d20\u7684\u4eae\u5ea6\uff1a<br \/>\n<dirtycode>PImage img;<\/p>\n<p>void setup() {<br \/>\n  size(320,214);<br \/>\n  img = loadImage( &#8220;1.jpg&#8221; );<br \/>\n}<\/p>\n<p>void draw() {<br \/>\n  loadPixels();<\/p>\n<p>  \/\/ We must also call loadPixels() on the PImage since we are going to read its pixels.  img.loadPixels();<br \/>\n  for (int x = 0; x < img.width; x++ ) {\n    for (int y = 0; y < img.height; y++ ) {\n      \n      \/\/ \u8ba1\u7b971D\u50cf\u7d20\u4f4d\u7f6e\n      int loc = x + y*img.width;\n\n      \/\/ \u83b7\u53d6\u56fe\u50cfR,G,B\u503c\n      float r = red (img.pixels[loc]);\n      float g = green (img.pixels[loc]);\n      float b = blue (img.pixels[loc]);\n      \n      \/\/ \u57fa\u4e8e\u4e0e\u9f20\u6807\u7684\u63a5\u8fd1\u5ea6\u8ba1\u7b97\u4e00\u4e2a\u603b\u91cf\u4ee5\u6539\u53d8\u4eae\u5ea6\n      float distance = dist(x,y,mouseX,mouseY);\n      \n      \/\/ \u8ddd\u79bb\u9f20\u6807\u8d8a\u8fd1\u7684\u50cf\u7d20, \u5176\"distance\"\u503c\u8d8a\u4f4e \n      \/\/ \u6211\u4eec\u8981\u66f4\u8fd1\u7684\u50cf\u7d20\u66f4\u4eae, \u56e0\u6b64\u6211\u4eec\u5c06\u516c\u5f0f\u7684\u503c\u53cd\u5411: float adjustBrightness = (50-distance)\/50 \n      \/\/ \u8ddd\u79bb\u9f20\u680750 (\u6216\u66f4\u5927)\u7684\u50cf\u7d20\u7684\u4eae\u5ea6\u4e3a0.0 (\u6216\u590d\u6570\uff0c\u8fd9\u91cc\u4e3a0)\n      \/\/ \u8ddd\u79bb\u9f20\u68070\u7684\u50cf\u7d20\u4eae\u5ea6\u4e3a1.0.\n      float adjustBrightness = (50-distance)\/50;\n      r *= adjustBrightness;\n      g *= adjustBrightness;\n      b *= adjustBrightness;\n      \n      \/\/ \u5c06RGB\u9650\u5236\u57280\uff5e255\n      r = constrain(r,0,255);\n      g = constrain(g,0,255);\n      b = constrain(b,0,255);\n      \n      \/\/ \u5236\u4f5c\u4e00\u4e2a\u65b0\u989c\u8272\u5e76\u5728\u7a97\u53e3\u8bbe\u7f6e\u50cf\u7d20\n      color c = color(r,g,b);\n      pixels[loc] = c;\n    }\n  }\n  \n  updatePixels();  \n}<\/dirtycode><\/p>\n<p>20. \u6211\u4eec\u4e4b\u524d\u7684\u4f8b\u5b50\u90fd\u662f\u4ece\u539f\u56fe\u8bfb\u53d6\u6bcf\u4e2a\u50cf\u7d20\u5e76\u4e14\u53ea\u63a5\u5728p5\u7a97\u53e3\u5185\u5199\u4e0b\u65b0\u50cf\u7d20\u3002\u7136\u800c\uff0c\u6211\u4eec\u5e38\u5e38\u4f7f\u7528\u4e00\u4e2a\u66f4\u65b9\u4fbf\u7684\u65b9\u5f0f\u5c06\u65b0\u7684\u50cf\u7d20\u5199\u5230\u4e00\u4e2a\u65b0\u7684\u76ee\u6807\u56fe\u50cf\u4e0a\uff08\u7136\u540e\u4f7f\u7528<em>image()<\/em>\u51fd\u6570\u663e\u793a\u51fa\u6765\uff09\u3002<\/p>\n<p>21. \u9600\u503c\uff08threshold\uff09\u6ee4\u955c\u4ec5\u4ee5\u4e24\u79cd\u72b6\u6001\u663e\u793a\u4e00\u4e2a\u56fe\u50cf\u7684\u6bcf\u4e2a\u50cf\u7d20\uff1a\u9ed1\u6216\u767d\uff08\u5176\u5b9e\u770b\u4e0b\u4f8b\uff0c\u6211\u4eec\u53ef\u4ee5\u5c06\u5b83\u4eec\u8bbe\u7f6e\u4e3a\u4efb\u610f\u7684\u4e24\u79cd\u989c\u8272\uff09\u3002\u9600\u503c\u53ef\u4ee5\u81ea\u5b9a\u4e49\uff0c\u5982\u679c\u50cf\u7d20\u7684\u4eae\u5ea6\u9ad8\u4e8e\u9600\u503c\uff0c\u5219\u4ee5\u767d\u8272\u6765\u663e\u793a\u4e4b\uff0c\u4f4e\u4e8e\u5219\u7528\u9ed1\u8272\u3002\u4e0b\u4f8b\u4e3a\u9600\u503c\u4e3a100\u7684\u60c5\u51b5\uff1a<br \/>\n   <dirtycode>PImage source;    \/\/ \u539f\u56fe<br \/>\n   PImage destination; \/\/ \u76ee\u6807\u56fe<\/p>\n<p>   void setup()  {<br \/>\n        size(200,200);<br \/>\n        source = loadImage( &#8220;fuckyoubaby.jpg&#8221; );<br \/>\n       \/\/\u76ee\u6807\u56fe\u88ab\u521b\u5efa\u4e3a\u4e00\u4e2a\u4e0e\u539f\u56fe\u5927\u5c0f\u76f8\u5f53\u7684\u7a7a\u767d\u56fe\u50cf<br \/>\n        destination = createImage(source.width, source.height, RGB);<br \/>\n    } <\/p>\n<p>   void draw()  {<br \/>\n        float threshold = 100;<br \/>\n        \/\/ \u6211\u4eec\u5c06\u770b\u4e00\u4e0b\u5168\u90e8\u56fe\u50cf\u7684\u50cf\u7d20<br \/>\n        source.loadPixels();<br \/>\n        destination.loadPixels(); <\/p>\n<p>    for (int x = 0; x < source.width; x++)  {   \n       for (int y = 0; y < source.height; y++)  {   \n          int loc = x + y*source.width;  \n         \/\/ \u57fa\u4e8e\u9600\u503c\u6bd4\u8f83\u4eae\u5ea6\n         \/\/ brightness()\u8fd4\u56de\u4e00\u4e2a0\uff5e255\u95f4\u7684\u503c\uff0c\u50cf\u7d20\u989c\u8272\u7684\u6574\u4f53\u4eae\u5ea6\u3002 \n          if (brightness(source.pixels[loc]) > threshold) {<br \/>\n            destination.pixels[loc] = color(255); \/\/ \u767d<br \/>\n           }  else  {<br \/>\n             destination.pixels[loc] =  color(0);   \/\/ \u9ed1<br \/>\n          }<br \/>\n      }<br \/>\n  }<br \/>\n      \/\/ \u57fa\u4e8e\u76ee\u6807\u56fe\u66f4\u6362\u50cf\u7d20<br \/>\n     destination.updatePixels();<br \/>\n      \/\/ \u663e\u793a\u76ee\u6807\u56fe<br \/>\n      image(destination,0,0);<br \/>\n   }<\/dirtycode><br \/>\n\u8fd9\u4e2a\u7279\u6b8a\u7684\u529f\u80fd\u5728p5\u7684<em>fliter()<\/em>\u51fd\u6570\u5185\u5e76\u4e0d\u53ef\u7528\u3002\u7406\u89e3\u66f4\u4f4e\u5c42\u4ee3\u7801\uff0c\u5728\u4f60\u60f3\u8981\u5728<em>fliter()<\/em>\u4e4b\u5916\u5236\u4f5c\u4f60\u81ea\u5df1\u60f3\u8981\u7684\u56fe\u50cf\u5904\u7406\u7b97\u6cd5\u65f6\u662f\u81f3\u5173\u91cd\u8981\u7684\u3002\u5982\u679c\u4f60\u60f3\u505a\u7684\u4ec5\u4ec5\u662f\u8bbe\u7f6e\u4e00\u4e2a\u9600\u503c\uff0c\u90a3\u4e48\u4e0b\u4f8b\u5c06\u66f4\u52a0\u7b80\u4fbf\uff1a<br \/>\n<dirtycode:noclick>\/\/ \u7ed8\u5236\u56fe\u50cf<br \/>\nimage(img,0,0);<br \/>\n\/\/ \u4f7f\u7528\u9600\u503c\u6ee4\u955c<br \/>\n\/\/ 0.5\u610f\u5473\u7740\u9600\u503c\u662f\u4eae\u5ea6\u768450%<br \/>\nfilter(THRESHOLD,0.5);<\/dirtycode><\/p>\n<p>22. <em>filter()<\/em>\u51fd\u6570\u63d0\u4f9b\u4e00\u7cfb\u5217\u6ee4\u955c\u3002\u8fd9\u5c06\u6539\u53d8\u56fe\u50cf\u663e\u793a\u7684\u6548\u679c\u3002\u6a21\u5f0f\u9664\u4e86THRESHOLD\uff0c\u8fd8\u6709GRAY, INVERT, POSTERIZE, BLUR, OPAQUE, ERODE \u548c DILATE\u3002\u53c2\u8003p5\u5b98\u7f51\u770b\u5b83\u4eec\u5404\u81ea\u7684\u4f8b\u5b50\u3002\u4f7f\u7528\u65b9\u6cd5\uff1a<br \/>\n<dirtycode:noclick>\ufb01lter(mode);<br \/>\n\ufb01lter(mode,level);<\/dirtycode><\/p>\n<p>23. \u4e4b\u524d\u6211\u4eec\u7684\u5b66\u4e60\u90fd\u662f\u57fa\u4e8e\u50cf\u7d20\u5bf9\u50cf\u7d20\uff0c\u800c\u4e3a\u4e86\u8fdb\u884c\u66f4\u8fdb\u4e00\u6b65\u7684\u56fe\u50cf\u5904\u7406\uff0c\u6211\u4eec\u5fc5\u987b\u8fdb\u5165\u50cf\u7d20\u7ec4\u5904\u7406\u3002<br \/>\n\u8ba9\u6211\u4eec\u5148\u4ece\u57fa\u4e8e\u539f\u56fe\u4e24\u4e2a\u50cf\u7d20\uff08\u4e00\u4e2a\u50cf\u7d20\u53ca\u5176\u5de6\u8fb9\u76f8\u90bb\u7684\u50cf\u7d20\uff09\u521b\u5efa\u4e00\u4e2a\u65b0\u50cf\u7d20\u5f00\u59cb\u3002<br \/>\n\u5982\u679c\u6211\u4eec\u7684\u50cf\u7d20\u4f4d\u4e8e(x,y)\uff1a<br \/>\n<dirtycode:noclick>int loc = x + y*img.width;<br \/>\ncolor pix = img.pixels[loc];<\/dirtycode><br \/>\n\u5b83\u5de6\u8fb9\u7684\u90bb\u5c45\u4fbf\u4f4d\u4e8e( x \uff0d 1, y):<br \/>\n<dirtycode:noclick>int leftLoc = (x-1) + y*img.width;<br \/>\ncolor leftPix = img.pixels[leftLoc];<\/dirtycode><br \/>\n\u63a5\u7740\u6211\u4eec\u57fa\u4e8e\u4e24\u4e2a\u50cf\u7d20\u95f4\u7684\u5dee\u5f02\u521b\u5efa\u4e00\u4e2a\u65b0\u989c\u8272\uff1a<br \/>\n<dirtycode:noclick>float diff = abs(brightness(pix) &#8211; brightness(leftPix));<br \/>\npixels[loc] = color(diff);<\/dirtycode>     <\/p>\n<p>\u4e0b\u4f8b\u662f\u5b8c\u5168\u7684\u7b97\u6cd5\uff1a<br \/>\n   <dirtycode>\/\/ \u7531\u4e8e\u6211\u4eec\u8981\u627e\u7684\u662f\u5de6\u90bb<br \/>\n   \/\/ \u56e0\u6b64\u6211\u4eec\u8df3\u8fc7\u7b2c\u4e00\u5217<br \/>\n   for (int x = 1; x < width; x++) {  \n     for (int y = 0; y < height; y++ ){  \n        \/\/ \u50cf\u7d20\u4f4d\u7f6e\u548c\u989c\u8272 \n        int loc = x + y*img.width; \n        color pix = img.pixels[loc];\n        \n        \/\/ \u5de6\u90bb\u50cf\u7d20\u7684\u4f4d\u7f6e\u548c\u989c\u8272 \n        int leftLoc = (x \u20131) + y*img.width; \n        color leftPix = img.pixels[leftLoc]; \n               \n        \/\/  \u50cf\u7d20\u53ca\u5176\u5de6\u90bb\u7684\u5dee\u662f\u65b0\u989c\u8272 \n        float diff = abs(brightness(pix) - brightness(leftPix));  \n        pixels[loc] = color(diff);  \n          }   \n     }<\/dirtycode><br \/>\n\u4e0a\u4f8b\u662f\u666e\u901a\u7684\u5782\u76f4\u8fb9\u7f18\u4fa6\u6d4b\u7b97\u6cd5\u3002\u5f53\u50cf\u7d20\u4e0e\u5176\u76f8\u90bb\u50cf\u7d20\u5dee\u522b\u5f88\u5927\u7684\u65f6\u5019\uff0c\u5b83\u4eec\u66f4\u50cf\u662f\u201c\u8fb9\u7f18\u201d\u50cf\u7d20\u3002\u4f8b\u5982\uff0c\u4f60\u5c06\u4e00\u5f20\u767d\u7eb8\u653e\u5728\u4e00\u4e2a\u9ed1\u8272\u7684\u684c\u9762\u4e0a\u3002\u7eb8\u7684\u8fb9\u7f18\u5373\u989c\u8272\u5dee\u5f02\u6700\u5927\u7684\u5730\u65b9\uff0c\u5373\u767d\u8272\u4e0e\u9ed1\u8272\u4ea4\u754c\u5904\u3002<\/p>\n<p>24. \u4e0a\u4f8b\u6211\u4eec\u67e5\u627e\u4e24\u4e2a\u50cf\u7d20\u627e\u5230\u8fb9\u7f18\u3002\u7136\u800c\u66f4\u590d\u6742\u7684\u7b97\u6cd5\u901a\u5e38\u662f\u5bfb\u627e\u66f4\u591a\u7684\u76f8\u90bb\u50cf\u7d20\u3002\u603b\u4e4b\uff0c\u6bcf\u4e2a\u50cf\u7d20\u90fd\u6709\u516b\u4e2a\u76f4\u63a5\u7684\u90bb\u5c45\uff1a\u4e0a\u4e0b\u5de6\u53f3\uff0c\u5de6\u4e0a\uff0c\u5de6\u4e0b\uff0c\u53f3\u4e0a\uff0c\u53f3\u4e0b\u3002\u8fd9\u4e9b\u56fe\u5f62\u5904\u7406\u7b97\u6cd5\u901a\u5e38\u88ab\u79f0\u4e3a\u201c\u7a7a\u95f4\u56de\u65cb\uff08spatial convolution\uff09\u201d\u3002\u5904\u7406\u4f7f\u7528\u4e00\u4e2a\u8f93\u5165\u50cf\u7d20\u53ca\u5176\u76f8\u90bb\u50cf\u7d20\u7684\u52a0\u6743\u5e73\u5747\u8ba1\u7b97\u4e00\u4e2a\u8f93\u51fa\u50cf\u7d20\u3002\u6362\u53e5\u8bdd\u8bf4\uff0c\u90a3\u4e2a\u65b0\u7684\u50cf\u7d20\u662f\u4e00\u4e2a\u533a\u57df\u5185\u50cf\u7d20\u7684\u51fd\u6570\u3002\u53ef\u4ee5\u4f7f\u7528\u4e0d\u540c\u7684\u5927\u5c0f\u7684\u76f8\u90bb\u533a\u57df\uff0c\u6bd4\u59823 \u00d7 3\u62165 \u00d7 5\u7684\u77e9\u9635\u7b49\u7b49\u3002<br \/>\n\u5404\u4e2a\u50cf\u7d20\u52a0\u6743\u7684\u4e0d\u540c\u7ec4\u5408\u5dee\u751f\u591a\u6837\u7684\u6548\u679c\u3002\u6bd4\u5982\uff0c\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u51cf\u5c0f\u76f8\u90bb\u50cf\u7d20\u503c\u5e76\u589e\u52a0\u4e2d\u5fc3\u70b9\u50cf\u7d20\u503c\u6765\u9510\u5316\u4e00\u4e2a\u56fe\u50cf\uff0c\u901a\u8fc7\u5e73\u5747\u6240\u6709\u76f8\u90bb\u50cf\u7d20\u503c\u6765\u6a21\u7cca\u4e00\u4e2a\u56fe\u50cf\u3002\uff08\u6ce8\u610f\u56de\u65cb\u77e9\u9635\u4e2d\u7684\u503c\u603b\u548c\u4e3a1\uff09<br \/>\n\u4e3e\u4f8b\uff0c<br \/>\n\u9510\u5316:<br \/>\n\u20131     \u20131      \u20131<br \/>\n\u20131       9      \u20131<br \/>\n\u20131     \u20131      \u20131<br \/>\n\u6a21\u7cca:<br \/>\n1\/9     1\/9     1\/9<br \/>\n1\/9     1\/9     1\/9<br \/>\n1\/9     1\/9     1\/9<\/p>\n<p>25. \u4e0b\u4f8b\u5c55\u793a\u4e86\u4e00\u4e2a\u4f7f\u75282D\u6570\u7ec4\u5b58\u50a8\u4e00\u4e2a3 \u00d7 3\u77e9\u9635\u50cf\u7d20\u52a0\u6743\u7684\u56de\u65cb\u3002\u8fd9\u53ef\u80fd\u4f7f\u6211\u4eec\u5230\u76ee\u524d\u4e3a\u6b62\u5728\u672c\u6559\u7a0b\u5185\u78b0\u5230\u7684\u6700\u5148\u8fdb\u7684\u4f8b\u5b50\uff0c\u56e0\u4e3a\u5b83\u4f7f\u7528\u4e86\u5982\u6b64\u591a\u7684\u5143\u7d20\uff08\u5d4c\u5957\u5faa\u73af\uff0c2D\u6570\u7ec4\uff0cPImage\u50cf\u7d20\uff0c\u7b49\u7b49\uff09\uff0c\u54e6\u4e5f\uff0c\u6211\u770b\u5f97\u4e5f\u5f88\u5d29\u6e83\uff0c\u56e0\u4e3a\u5b83\u592a\u957f\u4e86\uff0c\u653e\u8fd9\u6162\u6162\u7814\u7a76\u5427&#8230;<br \/>\n<dirtycode>PImage img;<br \/>\nint w = 80;<br \/>\n\/\/\u4e00\u4e2a&#8221;\u6a21\u7cca&#8221;\u6548\u679c\u7684\u56de\u65cb\u77e9\u9635\u4ee5\u4e00\u4e2a3 x 3\u4e8c\u7ef4\u6570\u7ec4\u50a8\u5b58.<br \/>\nfloat[][] matrix = { { 1.0\/9.0, 1.0\/9.0, 1.0\/9.0 } ,<br \/>\n                              { 1.0\/9.0, 1.0\/9.0, 1.0\/9.0 } ,<br \/>\n                              { 1.0\/9.0, 1.0\/9.0, 1.0\/9.0 } } ;<\/p>\n<p>void setup() {<br \/>\n  size(320,214);<br \/>\n  img = loadImage( &#8220;fuckyoubaby.jpg&#8221; );<br \/>\n}<\/p>\n<p>void draw() {<\/p>\n<p>  \/\/ \u6211\u4eec\u5c06\u4ec5\u5904\u7406\u56fe\u50cf\u7684\u4e00\u90e8\u5206<br \/>\n  \/\/ \u56e0\u6b64\u8ba9\u6211\u4eec\u5c06\u5168\u56fe\u8bbe\u4e3a\u80cc\u666f\u5148<br \/>\n  image(img,0,0);<\/p>\n<p>  \/\/ \u5728\u672c\u4f8b\u4e2d\u6211\u4eec\u4ec5\u4ec5\u5904\u7406\u56fe\u50cf\u7684\u4e00\u90e8\u5206\u2014\u4e00\u4e2a\u56f4\u7ed5\u9f20\u6807\u768480 x 80\u77e9\u5f62\uff08\u9f20\u6807\u5206\u522b\u5411\u4e0a\u4e0b\u5de6\u53f3\u6269\u5c5540\uff09.<br \/>\n  int xstart = constrain(mouseX-w\/2,0,img.width);<br \/>\n  int ystart = constrain(mouseY-w\/2,0,img.height);<br \/>\n  int xend = constrain(mouseX + w\/2,0,img.width);<br \/>\n  int yend = constrain(mouseY + w\/2,0,img.height);<br \/>\n  int matrixsize = 3;<\/p>\n<p>  loadPixels();<br \/>\n  \/\/ \u4e3a\u6bcf\u4e2a\u50cf\u7d20\u5f00\u59cb\u6211\u4eec\u7684\u5faa\u73af<br \/>\n  for (int x = xstart; x < xend; x++ ) {\n    for (int y = ystart; y < yend; y++ ) {\n      \/\/ \u6bcf\u4e2a\u50cf\u7d20\u4f4d\u7f6e(x,y)\u5f00\u59cb\u8fdb\u5165\u4e00\u4e2a\u540d\u4e3aconvolution()\u7684\u51fd\u6570\n      \/\/ convolution()\u51fd\u6570\u8fd4\u56de\u4e00\u4e2a\u65b0\u7684\u5f85\u663e\u793a\u7684\u989c\u8272.\n      color c = convolution(x,y,matrix,matrixsize,img); \n      int loc = x + y*img.width;\n      pixels[loc] = c;\n    }\n  }\n  updatePixels();\n  stroke(0);\n  noFill();\n  rect(xstart,ystart,w,w);\n}\n\ncolor convolution(int x, int y, float[][] matrix, int matrixsize, PImage img) {\n  float rtotal = 0.0;\n  float gtotal = 0.0;\n  float btotal = 0.0;\n  int offset = matrixsize \/ 2;\n  \n  \/\/ \u5728\u56de\u65cb\u77e9\u9635\u5185\u5faa\u73af\n  for (int i = 0; i < matrixsize; i++ ) {\n    for (int j = 0; j < matrixsize; j++ ) {\n      \n      \/\/ \u6211\u4eec\u6b63\u5728\u6d4b\u8bd5\u7684\u50cf\u7d20\u662f\uff1f\n      int xloc = x + i-offset;\n      int yloc = y + j-offset;\n      int loc = xloc + img.width*yloc;\n      \n      \/\/ \u786e\u4fdd\u6211\u4eec\u6ca1\u6709\u8d70\u51fa\u50cf\u7d20\u6570\u7ec4\u7684\u8fb9\u7f18\n      \/\/ \u5728\u67e5\u627e\u76f8\u90bb\u50cf\u7d20\u65f6\u786e\u4fdd\u6211\u4eec\u6ca1\u6709\u5076\u7136\u8d70\u51fa\u50cf\u7d20\u6570\u7ec4\u7684\u8fb9\u7f18\u5e38\u5e38\u662f\u5f88\u597d\u7684\u3002.\n      loc = constrain(loc,0,img.pixels.length-1);\n      \n      \/\/ \u8ba1\u7b97\u56de\u65cb\n      \/\/ \u6211\u4eec\u901a\u8fc7\u4e58\u4ee5\u56de\u65cb\u77e9\u9635\u7684\u503c\u4ee5\u5408\u8ba1\u6240\u6709\u7684\u76f8\u90bb\u50cf\u7d20.\n      rtotal += (red(img.pixels[loc]) * matrix[i][j]);\n      gtotal += (green(img.pixels[loc]) * matrix[i][j]);\n      btotal += (blue(img.pixels[loc]) * matrix[i][j]);\n    }\n  }\n  \n  \/\/ \u786e\u4fddRGB\u7684\u8303\u56f4\n  rtotal = constrain(rtotal,0,255);\n  gtotal = constrain(gtotal,0,255);\n  btotal = constrain(btotal,0,255);\n  \n  \/\/ \u8fd4\u56de\u7ed3\u679c\u989c\u8272\n  return color(rtotal,gtotal,btotal); \n}<\/dirtycode><\/p>\n<figure id=\"attachment_720\" aria-describedby=\"caption-attachment-720\" style=\"width: 319px\" class=\"wp-caption alignright\"><a href=\"https:\/\/learn.travelchinawith.me\/wp-content\/uploads\/2009\/11\/pr15-4.jpg\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/learn.travelchinawith.me\/wp-content\/uploads\/2009\/11\/pr15-4.jpg\" alt=\"\u968f\u673a\u70b9\u9635\" title=\"pr15-4\" width=\"319\" height=\"199\" class=\"size-full wp-image-720\" srcset=\"https:\/\/learn.travelchinawith.me\/wp-content\/uploads\/2009\/11\/pr15-4.jpg 319w, https:\/\/learn.travelchinawith.me\/wp-content\/uploads\/2009\/11\/pr15-4-300x187.jpg 300w\" sizes=\"auto, (max-width: 319px) 100vw, 319px\" \/><\/a><figcaption id=\"caption-attachment-720\" class=\"wp-caption-text\">\u968f\u673a\u70b9\u9635<\/figcaption><\/figure>\n<p>26. \u662f\u7684\uff0c\u4f60\u4f1a\u548c\u6211\u6709\u4e00\u6837\u7684\u7591\u95ee\uff0c\u4e0a\u8fb9\u8fd9\u4e9b\u96be\u9053PS\u4e0d\u53ef\u4ee5\u505a\u4e48\uff1f\u4e3a\u4ec0\u4e48\u8fd8\u8981\u5199\u8fd9\u4e48\u5927\u5806\u7684\u4ee3\u7801\uff1f\u662f\u7684\uff0c\u5b83\u4eec\u53ea\u662f\u5e2e\u52a9\u6211\u4eec\u53bb\u7406\u89e3\u505a\u4e00\u4e9b\u7279\u6548\u7684\u539f\u7406\uff0c\u4ee5\u53ca\u66f4\u5e95\u5c42\u7684\u6982\u5ff5\u2014\u2014\u50cf\u7d20\u3002\u4ee5\u4e0b\u51e0\u4e2a\u4f8b\u5b50\u5728PS\u91cc\u5c31\u5f88\u96be\u5b8c\u6210\u4e86\u3002\u629b\u5f00\u7e41\u6742\u7684\u7f16\u7801\uff0c\u770b\u4e0b\u9762\u8fd9\u4e2a\u7ed8\u5236p5\u5f62\u72b6\u7684\u7b97\u6cd5\u3002\u6bcf\u5faa\u73af\u4e00\u6b21<em>draw()<\/em>\uff0c\u6211\u4eec\u4ee5\u968f\u673a\u7684\u4f4d\u7f6e\u753b\u4e0b\u4e00\u4e2a\u5706\uff0c\u5404\u4e2a\u5706\u7684\u989c\u8272\u53d6\u81ea\u539f\u56fe\u5bf9\u5e94\u4f4d\u7f6e\u7684\u989c\u8272\uff0c\u6765\u521b\u5efa\u4e00\u79cd\u70b9\u9635\u56fe\u7684\u7279\u6548\u3002<br \/>\n<dirtycode:noclick>PImage img;<br \/>\nint pointillize = 10;<\/p>\n<p>void setup() {<br \/>\n  size(320,214);<br \/>\n  img = loadImage(&#8220;fuckyoubaby.jpg&#8221;);<br \/>\n  background(255);<br \/>\n  smooth();<br \/>\n}<\/p>\n<p>void draw() {<\/p>\n<p>  \/\/ \u9009\u53d6\u4e00\u4e2a\u968f\u673a\u70b9<br \/>\n  int x = int(random(img.width));<br \/>\n  int y = int(random(img.height));<br \/>\n  int loc = x + y*img.width;<\/p>\n<p>  \/\/ \u4ece\u539f\u56fe\u67e5\u627eRGB\u989c\u8272<br \/>\n  loadPixels();<br \/>\n  float r = red(img.pixels[loc]);<br \/>\n  float g = green(img.pixels[loc]);<br \/>\n  float b = blue(img.pixels[loc]);<\/p>\n<p>  \/\/ \u56de\u5230\u5f62\u72b6! \u76f8\u5bf9\u4e8e\u8bbe\u7f6e\u4e00\u4e2a\u50cf\u7d20, \u6211\u4eec\u4f7f\u7528\u4e00\u4e2a\u50cf\u7d20\u7684\u989c\u8272\u6765\u753b\u4e00\u4e2a\u5706.<br \/>\n  noStroke();<br \/>\n  fill(r,g,b,100);<br \/>\n  ellipse(x,y,pointillize,pointillize);<br \/>\n}<\/dirtycode><br \/>\n<figure id=\"attachment_721\" aria-describedby=\"caption-attachment-721\" style=\"width: 314px\" class=\"wp-caption alignright\"><a href=\"https:\/\/learn.travelchinawith.me\/wp-content\/uploads\/2009\/11\/pr15-5.jpg\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/learn.travelchinawith.me\/wp-content\/uploads\/2009\/11\/pr15-5.jpg\" alt=\"2D\u8f6c3D\" title=\"pr15-5\" width=\"314\" height=\"212\" class=\"size-full wp-image-721\" srcset=\"https:\/\/learn.travelchinawith.me\/wp-content\/uploads\/2009\/11\/pr15-5.jpg 314w, https:\/\/learn.travelchinawith.me\/wp-content\/uploads\/2009\/11\/pr15-5-300x202.jpg 300w\" sizes=\"auto, (max-width: 314px) 100vw, 314px\" \/><\/a><figcaption id=\"caption-attachment-721\" class=\"wp-caption-text\">2D\u8f6c3D<\/figcaption><\/figure><\/p>\n<p>27. \u518d\u770b\u4e00\u4e2a\u4f8b\u5b50\uff0c\u6211\u4eec\u4ece\u4e00\u4e2a\u4e8c\u7ef4\u56fe\u50cf\u4e2d\u83b7\u53d6\u6570\u636e\uff0c\u75283D\u4f4d\u79fb\u6280\u672f\u5728\u4e09\u7ef4\u7a7a\u95f4\u5185\u7684\u6bcf\u4e00\u4e2a\u50cf\u7d20\u6e32\u67d3\u4e00\u4e2a\u77e9\u5f62\u3002z\u7684\u4f4d\u7f6e\u53d6\u51b3\u4e8e\u989c\u8272\u7684\u4eae\u5ea6\u3002\u8d8a\u4eae\u7684\u770b\u8d77\u6765\u8d8a\u9760\u8fd1\u89c2\u8005\uff0c\u8d8a\u6697\u7684\u5219\u8d8a\u8fdc\u3002<br \/>\n<dirtycode:noclick>PImage img;       \/\/ \u539f\u56fe<br \/>\nint cellsize = 2; \/\/ \u70b9\u9635\u6bcf\u4e2a\u5355\u5143\u7684\u5c3a\u5bf8<br \/>\nint cols, rows;   \/\/ \u6211\u4eec\u7cfb\u7edf\u4e2d\u884c\u548c\u5217\u7684\u6570\u76ee<\/p>\n<p>void setup() {<br \/>\n  size(320,214,P3D);<br \/>\n  img = loadImage( &#8220;fuckyoubaby.jpg&#8221; ); \/\/ \u8f7d\u5165\u56fe\u50cf<br \/>\n  cols = width\/cellsize;              \/\/ \u8ba1\u7b97\u5217\u7684\u6570\u76ee<br \/>\n  rows = height\/cellsize;             \/\/ \u8ba1\u7b97\u884c\u7684\u6570\u76ee<br \/>\n}<\/p>\n<p>void draw() {<br \/>\n  background(255);<br \/>\n  img.loadPixels();<\/p>\n<p>  \/\/ \u5f00\u59cb\u5faa\u73af\u5217<br \/>\n  for (int i = 0; i < cols; i++ ) {\n    \/\/ \u5f00\u59cb\u5faa\u73af\u884c\n    for (int j = 0; j < rows; j++ ) {\n      int x = i*cellsize + cellsize\/2; \/\/ x\u4f4d\u7f6e\n      int y = j*cellsize + cellsize\/2; \/\/ y\u4f4d\u7f6e\n      int loc = x + y*width;           \/\/ \u50cf\u7d20\u6570\u7ec4\u4f4d\u7f6e\n      color c = img.pixels[loc];       \/\/ \u622a\u53d6\u989c\u8272\n\n      \/\/ \u4f9dmouseX\u548c\u50cf\u7d20\u4eae\u5ea6\u8ba1\u7b97z\u4f4d\u7f6e\n      float z = (mouseX\/(float)width) * brightness(img.pixels[loc])- 100.0;\n\n      \/\/ \u4f4d\u79fb\u5230\u76f8\u5e94\u4f4d\u7f6e, \u8bbe\u7f6e\u586b\u5145\u548c\u8fb9\u6846, \u5e76\u7ed8\u5236\u77e9\u5f62\n      pushMatrix();\n      translate(x,y,z); \n      fill(c);\n      noStroke();\n      rectMode(CENTER);\n      rect(0,0,cellsize,cellsize);\n      popMatrix();\n    }\n  }\n}<\/dirtycode><\/p>\n<figure id=\"attachment_722\" aria-describedby=\"caption-attachment-722\" style=\"width: 319px\" class=\"wp-caption alignright\"><a href=\"https:\/\/learn.travelchinawith.me\/wp-content\/uploads\/2009\/11\/pr15-6.jpg\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/learn.travelchinawith.me\/wp-content\/uploads\/2009\/11\/pr15-6.jpg\" alt=\"\u4e09\u89d2\u4e09\u89d2\uff01\" title=\"pr15-6\" width=\"319\" height=\"199\" class=\"size-full wp-image-722\" srcset=\"https:\/\/learn.travelchinawith.me\/wp-content\/uploads\/2009\/11\/pr15-6.jpg 319w, https:\/\/learn.travelchinawith.me\/wp-content\/uploads\/2009\/11\/pr15-6-300x187.jpg 300w\" sizes=\"auto, (max-width: 319px) 100vw, 319px\" \/><\/a><figcaption id=\"caption-attachment-722\" class=\"wp-caption-text\">\u4e09\u89d2\u4e09\u89d2\uff01<\/figcaption><\/figure>\n<p>28. \u662f\u7684\uff0c\u53ef\u4ee5\u7528\u4efb\u4f55\u56fe\u5f62\u8fdb\u884c\u7c7b\u4f3c\u7684\u586b\u5145\uff0c\u6bd4\u5982\u4e09\u89d2\u5f62\uff1a<br \/>\n<dirtycode:noclick>PImage img;<\/p>\n<p>void setup() {<br \/>\n  size(320, 214);<br \/>\n  \/\/ \u8f7d\u5165\u56fe\u50cf<br \/>\n  img = loadImage(&#8220;fuckyoubaby.jpg&#8221;);<br \/>\n  smooth();<br \/>\n}<\/p>\n<p>void draw() {<br \/>\n  background(255);<br \/>\n  \/\/ \u5904\u7406\u50cf\u7d20\u524d\u5148\u547c\u53ebloadPixels<br \/>\n  loadPixels();<br \/>\n  \/\/ \u6cbfy\u8f74\u6bcf\u969410\u4e2a\u50cf\u7d20\u5faa\u73af\u6240\u6709\u50cf\u7d20<br \/>\n  for (int y = 0; y < img.height; y+=10 ) {\n    \/\/ \u6cbfx\u8f74\u6bcf\u96945\u4e2a\u50cf\u7d20\u5faa\u73af\u6240\u6709\u50cf\u7d20\n    for (int x = 0; x < img.width+5; x+=5) {\n      \/\/ \u4ece\u4e00\u4e2a2D\u7f51\u683c\u8ba1\u7b971D\u4f4d\u7f6e\n      int loc = x + y*img.width;\n      \/\/ \u4ee5\u539f\u56fe\u586b\u8272\n      stroke(img.pixels[loc]);\n      fill(img.pixels[loc]);\n      \/\/ \u9876\u70b9\u671d\u4e0a\u6216\u9876\u70b9\u671d\u4e0b\u7ed8\u5236\u4e09\u89d2\u5f62\n      if (x %10 == 0) triangle(x-5,y,x,y+10,x+5,y);\n      else triangle(x-5,y+10,x,y,x+5,y+10);\n    }\n  }\n}<\/dirtycode><br \/>\n\u57fa\u4e8e\u4e0a\u4f8b\u6362\u4e00\u4e9b\u5176\u4ed6\u5f62\u72b6\u8fdb\u53bb\uff0c\u4f60\u4f1a\u5f97\u5230\u5f88\u591a\u6709\u8da3\u7684\u3001PS\u96be\u4ee5\u505a\u5230\u7684\u7279\u6548\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>1. \u8f7d\u5165\u4e00\u5f20\u56fe\u7247\uff1a \/\/ \u7533\u660e\u4e00\u4e2aPImage\u7c7b\u578b\u7684\u53d8\u91cf, \u4e00\u4e2a\u6211\u4eec\u53ef\u7528\u7684\u5b58\u50a8\u4e8ep5\u6838\u5fc3\u5e93\u5185\u7684\u7c7b. PImage img; void setup() { size(320,240); \/\/ \u901a\u8fc7\u8f7d\u5165\u4e00\u4e2a\u56fe\u7247\u6587\u4ef6\u521b\u5efa\u4e00\u4e2a\u65b0\u7684PImage\u5b9e\u4f8b img = loadImage(&#8220;fuckyoubaby.jpg&#8221;); } void draw() { background(0); \/\/image()\u51fd\u6570\u5728\u4e00\u4e2a\u4f4d\u7f6e\u663e\u793a\u56fe\u7247-\u672c\u4f8b\u4f4d\u4e8e\u70b9(0,0). image(img,0,0); } \u8fd9\u91cc\uff0c\u624b\u8d31\u7684\u6211\u53c8\u53d1\u73b0\u4e00\u70b9\uff1a\u5982\u679c\u4f60\u5c06\u4f60\u7684\u8fd9\u4e2asketch\u547d\u540d\u4e3aPImage\u7684\u8bdd\uff0c\u70b9\u51fb\u8fd0\u884cp5\u4f1a\u8fd4\u56de\u4f60\u9519\u8bef\uff1a&#8221;cannot convert from PImage to PImage&#8221;\u3002\u3002\u3002\u76f4\u5230\u4f60\u91cd\u547d\u540d\u540e\u624d\u80fd\u6b63\u5e38\u8fd0\u884c\u3002 2. \u5728\u4e0a\u4f8b\u5185\u653e\u5165\u4e00\u5f20\u6bd4size\u5927\u7684\u56fe\u7247\uff0c\u8fd0\u884c\uff0c\u5b83\u5c06\u4ee5\u539f\u5c3a\u5bf8\u4ece\u5de6\u4e0a\u89d2\u5f00\u59cb\u663e\u793a\u56fe\u7247\uff0c\u4f60\u7684size\u6709\u591a\u5927\uff0c\u4ed6\u5c31\u663e\u793a\u591a\u5927\uff0c\u6ca1\u6709\u7f29\u653e\u3002 3. \u8981\u663e\u793a\u7684\u56fe\u7247\u5fc5\u987b\u4f4d\u4e8e\u672csketch\u6587\u4ef6\u5939\u4e0b\u7684data\u6587\u4ef6\u5939\u5185\uff08\u8fd9\u4e5f\u662fp5\u8f7d\u5165\u5916\u90e8\u6587\u4ef6\u7684\u7edf\u4e00\u89c4\u77e9\uff09\u3002 4. \u6211\u4eec\u9700\u8981\u6ce8\u610f\uff0c\u4ece\u786c\u76d8\u8f7d\u5165\u56fe\u7247\u76f8\u5bf9\u6765\u8bf4\u662f\u8f83\u6162\u7684\uff0c\u56e0\u6b64\u6211\u4eec\u5e94\u5f53\u786e\u4fdd\u6211\u4eec\u7684\u7a0b\u5e8f\u4ec5\u6267\u884c\u5176\u4e00\u6b21\uff0c\u56e0\u6b64\uff0c\u5e94\u5f53\u5c06\u5b83\u653e\u5230setup()\u800c\u4e0d\u662fdraw()\u91cc\u3002 5. \u4e00\u65e6\u56fe\u7247\u88ab\u8f7d\u5165\uff0c\u5b83\u5c06\u88abimage()\u51fd\u6570\u663e\u793a\u51fa\u6765\uff0c\u8fd9\u4e2a\u51fd\u6570\u5fc5\u5907\u4e09\u4e2a\u5f15\u6570\uff1a\u88ab\u663e\u793a\u7684\u56fe\u7247\u3001x\u4f4d\u7f6e\u548cy\u4f4d\u7f6e\uff0c\u53e6\u5916\uff0c\u540e\u8fb9\u8fd8\u53ef\u63a5\u4e24\u4e2a\u5907\u9009\u53c2\u6570\uff1a\u56fe\u50cf\u7684\u5bbd\u548c\u9ad8\uff08\u5168\u56fe\u7f29\u653e\uff09\u3002 6. \u8ba9\u56fe\u7247\u52a8\u8d77\u6765\uff1a PImage dick; \/\/ \u4e3a\u56fe\u7247\u6587\u4ef6\u51c6\u5907\u7684\u53d8\u91cf float x,y; \/\/ \u56fe\u7247\u4f4d\u7f6e\u53d8\u91cf float rot; \/\/ \u56fe\u7247\u65cb\u8f6c\u53d8\u91cf void setup() { [&hellip;]<\/p>\n","protected":false},"author":4,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[57],"tags":[58],"class_list":["post-716","post","type-post","status-publish","format-standard","hentry","category-processing","tag-processing"],"blocksy_meta":[],"_links":{"self":[{"href":"https:\/\/learn.travelchinawith.me\/index.php?rest_route=\/wp\/v2\/posts\/716","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/learn.travelchinawith.me\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/learn.travelchinawith.me\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/learn.travelchinawith.me\/index.php?rest_route=\/wp\/v2\/users\/4"}],"replies":[{"embeddable":true,"href":"https:\/\/learn.travelchinawith.me\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=716"}],"version-history":[{"count":7,"href":"https:\/\/learn.travelchinawith.me\/index.php?rest_route=\/wp\/v2\/posts\/716\/revisions"}],"predecessor-version":[{"id":729,"href":"https:\/\/learn.travelchinawith.me\/index.php?rest_route=\/wp\/v2\/posts\/716\/revisions\/729"}],"wp:attachment":[{"href":"https:\/\/learn.travelchinawith.me\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=716"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/learn.travelchinawith.me\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=716"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/learn.travelchinawith.me\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=716"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}