📄 genmath.java
字号:
// get polygon Point [] in = new Point[count*2]; for(int i=0; i<count*2; i++) { in[i] = new Point(); if (i < count) in[i].setLocation(points[i]); } Point [] out = new Point[count*2]; for(int i=0; i<count*2; i++) out[i] = new Point(); // clip on all four sides Point [] a = in; Point [] b = out; if ((pre & LEFT) != 0) { count = clipEdge(a, count, b, LEFT, lx); Point [] swap = a; a = b; b = swap; } if ((pre & RIGHT) != 0) { count = clipEdge(a, count, b, RIGHT, hx); Point [] swap = a; a = b; b = swap; } if ((pre & TOP) != 0) { count = clipEdge(a, count, b, TOP, hy); Point [] swap = a; a = b; b = swap; } if ((pre & BOTTOM) != 0) { count = clipEdge(a, count, b, BOTTOM, ly); Point [] swap = a; a = b; b = swap; } // remove redundant points from polygon pre = 0; for(int i=0; i<count; i++) { if (i > 0 && a[i-1].x == a[i].x && a[i-1].y == a[i].y) continue; b[pre].x = a[i].x; b[pre].y = a[i].y; pre++; } // closed polygon: remove redundancy on wrap-around while (pre != 0 && b[0].x == b[pre-1].x && b[0].y == b[pre-1].y) pre--; count = pre; // copy the polygon back if it in the wrong place Point [] retArr = new Point[count]; for(int i=0; i<count; i++) retArr[i] = b[i]; return retArr; } /** * Method to clip polygon "in" against line "edge" (1:left, 2:right, * 4:bottom, 8:top) and place clipped result in "out". */ private static int clipEdge(Point [] in, int inCount, Point [] out, int edge, int value) { // look at all the lines Point first = new Point(); Point second = new Point(); int firstx = 0, firsty = 0; int outcount = 0; for(int i=0; i<inCount; i++) { int pre = i - 1; if (i == 0) pre = inCount-1; first.setLocation(in[pre]); second.setLocation(in[i]); if (clipSegment(first, second, edge, value)) continue; int x1 = first.x; int y1 = first.y; int x2 = second.x; int y2 = second.y; if (outcount != 0) { if (x1 != out[outcount-1].x || y1 != out[outcount-1].y) { out[outcount].x = x1; out[outcount++].y = y1; } } else { firstx = x1; firsty = y1; } out[outcount].x = x2; out[outcount++].y = y2; } if (outcount != 0 && (out[outcount-1].x != firstx || out[outcount-1].y != firsty)) { out[outcount].x = firstx; out[outcount++].y = firsty; } return outcount; } /** * Method to do clipping on the vector from (x1,y1) to (x2,y2). * If the vector is completely invisible, true is returned. */ private static boolean clipSegment(Point p1, Point p2, int codebit, int value) { int x1 = p1.x; int y1 = p1.y; int x2 = p2.x; int y2 = p2.y; int c1 = 0, c2 = 0; if (codebit == LEFT) { if (x1 < value) c1 = codebit; if (x2 < value) c2 = codebit; } else if (codebit == BOTTOM) { if (y1 < value) c1 = codebit; if (y2 < value) c2 = codebit; } else if (codebit == RIGHT) { if (x1 > value) c1 = codebit; if (x2 > value) c2 = codebit; } else if (codebit == TOP) { if (y1 > value) c1 = codebit; if (y2 > value) c2 = codebit; } if (c1 == c2) return c1 != 0; boolean flip = false; if (c1 == 0) { int t = x1; x1 = x2; x2 = t; t = y1; y1 = y2; y2 = t; flip = true; } if (codebit == LEFT || codebit == RIGHT) { long t = (y2-y1); t *= (value-x1); t /= (x2-x1); y1 += t; x1 = value; } else if (codebit == BOTTOM || codebit == TOP) { long t = (x2-x1); t *= (value-y1); t /= (y2-y1); x1 += t; y1 = value; } if (flip) { p1.x = x2; p1.y = y2; p2.x = x1; p2.y = y1; } else { p1.x = x1; p1.y = y1; p2.x = x2; p2.y = y2; } return false; } /** * Returns the closest <code>long</code> to the argument. The result * is rounded to an integer by adding 1/2, taking the floor of the * result, and casting the result to type <code>long</code>. In other * words, the result is equal to the value of the expression: * <p><pre>(long)Math.floor(a + 0.5d)</pre> * <p> * Special cases: * <ul><li>If the argument is NaN, the result is 0. * <li>If the argument is negative infinity or any value less than or * equal to the value of <code>Long.MIN_VALUE</code>, the result is * equal to the value of <code>Long.MIN_VALUE</code>. * <li>If the argument is positive infinity or any value greater than or * equal to the value of <code>Long.MAX_VALUE</code>, the result is * equal to the value of <code>Long.MAX_VALUE</code>.</ul> * * @param x a floating-point value to be rounded to a <code>long</code>. * @return the value of the argument rounded to the nearest <code>long</code> value. * @see java.lang.Long#MAX_VALUE * @see java.lang.Long#MIN_VALUE */ public static long roundLong(double x) { return (long)(x >= 0 ? x + HALF : x - HALF); } public static double rint(double x) { double twoToThe52 = (1L << 52); // 2^52 if (x >= 0) { if (x < twoToThe52) return (twoToThe52 + x) - twoToThe52; } else { if (x > -twoToThe52) return (-twoToThe52 + x) + twoToThe52; } return x; } public static int roundInt(double x) { return (int)(x >= 0 ? x + HALF : x - HALF); } public static long floorLong(double x) { long v = (long)x; return x >= 0 || v == x ? v : x > Long.MIN_VALUE ? v - 1 : Long.MIN_VALUE; } public static long ceilLong(double x) { long v = (long)x; return x <= 0 || v == x ? v : x < Long.MAX_VALUE ? v + 1 : Long.MAX_VALUE; } public static int floorInt(double x) { int v = (int)x; return x >= 0 || v == x ? v : x > Integer.MIN_VALUE ? v - 1 : Integer.MIN_VALUE; } public static int ceilInt(double x) { int v = (int)x; return x <= 0 || v == x ? v : x < Integer.MAX_VALUE ? v + 1 : Integer.MAX_VALUE; } private static final double SIN30 = 0.5; private static final double SIN45 = 0.7071067811865476; private static final double [] sineTable = { 0.0,0.0017453283658983088,0.003490651415223732,0.00523596383141958,0.0069812602979615525,0.008726535498373935, 0.010471784116245792,0.012217000835247169,0.013962180339145272,0.015707317311820675,0.01745240643728351,0.019197442399689665, 0.020942419883356957,0.022687333572781358,0.024432178152653153,0.02617694830787315,0.02792163872356888,0.029666244085110757, 0.03141075907812829,0.033155178388526274,0.03489949670250097,0.036643708706556276,0.03838780908751994,0.04013179253255973, 0.04187565372919962,0.043619387365336,0.04536298812925378,0.04710645070964266,0.04884976979561326,0.05059294007671331, 0.05233595624294383,0.05407881298477529,0.0558215049931638,0.057564026959567284,0.05930637357596162,0.061048539534856866, 0.06279051952931337,0.06453230825295798,0.06627390040000014,0.06801529066524817,0.0697564737441253,0.07149744433268591, 0.07323819712763169,0.0749787268263277,0.07671902812681863,0.07845909572784494,0.08019892432885892,0.08193850863004093, 0.08367784333231548,0.08541692313736746,0.08715574274765817,0.08889429686644151,0.09063258019778016,0.09237058744656158, 0.09410831331851431,0.095845752520224,0.09758289975914947,0.099319749743639,0.10105629718294634,0.1027925367872468, 0.10452846326765346,0.1062640713362332,0.10799935570602284,0.10973431109104527,0.11146893220632548,0.11320321376790671, 0.11493715049286661,0.11667073709933316,0.11840396830650095,0.1201368388346471,0.12186934340514746,0.12360147674049271, 0.12533323356430426,0.12706460860135046,0.1287955965775628,0.13052619222005157,0.13225639025712244,0.13398618541829205, 0.13571557243430438,0.13744454603714665,0.13917310096006544,0.14090123193758267,0.14262893370551163,0.1443562010009732, 0.14608302856241162,0.14780941112961063,0.14953534344370953,0.1512608202472192,0.15298583628403806,0.1547103862994681, 0.15643446504023087,0.15815806725448353,0.15988118769183485,0.1616038211033611,0.16332596224162227,0.16504760586067765, 0.16676874671610226,0.16848937956500257,0.17020949916603254,0.17192910027940955,0.17364817766693033,0.1753667260919871, 0.1770847403195833,0.17880221511634958,0.18051914525055998,0.18223552549214747,0.18395135061272017,0.1856666153855772, 0.1873813145857246,0.18909544298989128,0.19080899537654483,0.19252196652590742,0.19423435121997196,0.1959461442425177, 0.19765734037912613,0.1993679344171972,0.20107792114596468,0.2027872953565125,0.20449605184179032,0.20620418539662963, 0.20791169081775931,0.20961856290382183,0.21132479645538865,0.21303038627497656,0.2147353271670632,0.21643961393810285, 0.21814324139654254,0.21984620435283753,0.2215484976194673,0.22325011601095135,0.22495105434386498,0.22665130743685505, 0.22835087011065575,0.2300497371881044,0.23174790349415733,0.2334453638559054,0.23514211310259,0.23683814606561868, 0.23853345757858088,0.24022804247726373,0.2419218955996677,0.2436150117860225,0.24530738587880258,0.2469990127227429, 0.2486898871648548,0.2503800040544414,0.2520693582431136,0.25375794458480566,0.25544575793579055,0.25713279315469617, 0.25881904510252074,0.26050450864264835,0.2621891786408647,0.26387304996537286,0.2655561174868088,0.26723837607825685, 0.2689198206152657,0.27060044597586363,0.27228024704057435,0.27395921869243245,0.27563735581699916,0.2773146533023778, 0.2789911060392293,0.28066670892078777,0.2823414568428764,0.2840153447039226,0.28568836740497355,0.287360519849712, 0.2890317969444716,0.2907021935982525,0.29237170472273677,0.29404032523230395,0.29570805004404666,0.29737487407778596, 0.29904079225608665,0.3007057995042731,0.30236989075044446,0.3040330609254903,0.30569530496310565,0.30735661779980705, 0.3090169943749474,0.3106764296307318,0.31233491851223255,0.31399245596740494,0.31564903694710245,0.3173046564050921, 0.3189593092980699,0.32061299058567627,0.3222656952305111,0.3239174181981494,0.3255681544571567,0.3272178989791039, 0.32886664673858323,0.330514392713223,0.3321611318837033,0.3338068592337709,0.335451569750255,0.3370952584230821, 0.3387379202452914,0.34037955021305016,0.3420201433256687,0.34365969458561607,0.34529819899853464,0.34693565157325584, 0.3485720473218152,0.35020738125946743,0.3518416484047018,0.3534748437792571,0.35510696240813705,0.3567379993196252, 0.35836794954530027,0.3599968081200512,0.3616245700820923,0.36325123047297836,0.3648767843376196,0.36650122672429725, 0.3681245526846779,0.3697467572738293,0.3713678355502348,0.37298778257580895,0.37460659341591207,0.3762242631393656, 0.3778407868184671,0.3794561595290051,0.3810703763502741,0.3826834323650898,0.3842953226598037,0.38590604232431863, 0.38751558645210293,0.38912395014020623,0.39073112848927377,0.39233711660356146,0.3939419095909511,0.39554550256296495, 0.3971478906347806,0.3987490689252462,0.4003490325568949,0.4019477766559601,0.40354529635239,0.4051415867798625, 0.40673664307580015,0.40833046038138493,0.4099230338415728,0.41151435860510877,0.41310442982454176,0.414693242656239, 0.41628079226040116,0.41786707380107674,0.4194520824461771,0.421035813367491,0.4226182617406994,0.4241994227453902, 0.42577929156507266,0.4273578633871924,0.4289351334031459,0.43051109680829514,0.4320857488019823,0.43365908458754426, 0.43523109937232746,0.43680178836770217,0.4383711467890774,0.4399391698559151,0.4415058527917452,0.44307119082417973, 0.4446351791849275,0.44619781310980877,0.44775908783876966,0.4493189986158966,0.45087754068943076,0.4524347093117827, 0.45399049973954675,0.4555449072335155,0.4570979270586942,0.45864955448431494,0.46019978478385165,0.4617486132350339, 0.4632960351198617,0.4648420457246196,0.4663866403398912,0.46792981426057334,0.46947156278589075,0.47101188121940996, 0.47255076486905395,0.4740882090471163,0.47562420907027525,0.4771587602596084,0.4786918579406068,0.48022349744318893, 0.4817536741017153,0.4832823832550024,0.48480962024633695,0.48633538042349045,0.4878596591387326,0.4893824517488462, 0.4909037536151409,0.49242356010346716,0.493941866584231,0.4954586684324075,0.49697396102755526,0.49848773975383026, SIN30, 0.5015107371594573,0.503019946630235,0.5045276238150193,0.5060337641211637,0.5075383629607041, 0.5090414157503713,0.5105429179116057,0.5120428648705715,0.51354125205817,0.5150380749100542,0.5165333288666418, 0.5180270093731302,0.5195191118795094,0.5210096318405764,0.5224985647159488,0.5239859059700791,0.5254716510722678, 0.5269557954966776,0.5284383347223471,0.5299192642332049,0.5313985795180829,0.53287627607073,0.5343523493898263, 0.5358267949789967,0.5372996083468239,0.5387707850068629,0.540240320477655,0.5417082102827397,0.5431744499506707, 0.544639035015027,0.5461019610144291,0.5475632234925503,0.5490228179981317,0.5504807400849956,0.5519369853120581, 0.5533915492433441,0.5548444274479992,0.5562956155003048,0.5577451089796901,0.5591929034707469,0.5606389945632416, 0.5620833778521306,0.5635260489375715,0.5649670034249379,0.5664062369248328,0.5678437450531012,0.5692795234308442, 0.5707135676844316,0.5721458734455162,0.573576436351046,0.5750052520432786,0.5764323161697932,0.5778576243835053, 0.5792811723426788,0.5807029557109398,0.5821229701572894,0.5835412113561175,0.5849576749872154,0.5863723567357892, 0.5877852522924731,0.589196357353342,0.5906056676199254,0.5920131787992196,0.5934188866037015,0.5948227867513413, 0.5962248749656158,0.5976251469755212,0.5990235985155858,0.600420225325884,0.6018150231520482,0.6032079877452825, 0.6045991148623747,0.605988400265711,0.6073758397232867,0.6087614290087207,0.6101451639012676,0.6115270401858311, 0.6129070536529765,0.6142852000989432,0.6156614753256583,0.6170358751407485,0.6184083953575542,0.61977903179514, 0.6211477802783103,0.6225146366376195,0.6238795967093861,0.6252426563357052,0.6266038113644604,0.6279630576493379, 0.6293203910498374,0.6306758074312863,0.6320293026648508,0.6333808726275502,0.6347305132022676,0.636078220277764, 0.6374239897486897,0.6387678175155976,0.6401096994849556,0.6414496315691578,0.6427876096865393,0.6441236297613865, 0.6454576877239505,0.6467897795104596,0.6481199010631309,0.6494480483301835,0.6507742172658509,0.6520984038303922, 0.6534206039901054,0.6547408137173397,0.6560590289905073,0.6573752457940958,0.6586894601186803,0.6600016679609367, 0.6613118653236518,0.6626200482157375,0.6639262126522416,0.6652303546543609,0.6665324702494525,0.6678325554710466, 0.6691306063588582,0.6704266189587991,0.6717205893229902,0.6730125135097733,0.6743023875837234,0.6755902076156601, 0.6768759696826607,0.6781596698680706,0.6794413042615165,0.6807208689589178,0.6819983600624985,0.6832737736807992, 0.6845471059286886,0.6858183529273763,0.687087510804423,0.6883545756937539,0.6896195437356697,0.6908824110768583, 0.6921431738704068,0.693401828275813,0.6946583704589974,0.6959127965923143,0.6971651028545645,0.6984152854310058, 0.6996633405133654,0.7009092642998509,0.7021530529951624,0.7033947028105039,0.7046342099635946,0.705871570678681, SIN45, 0.7083398377245288,0.7095707365365209,0.7107994738729925,0.7120260459909965,0.7132504491541816, 0.7144726796328033,0.7156927337037359,0.7169106076504826,0.7181262977631888,0.7193398003386512,0.7205511116803304, 0.7217602280983622,0.7229671459095681,0.7241718614374675,0.7253743710122875,0.7265746709709759,0.7277727576572104, 0.7289686274214116,0.7301622766207523,0.7313537016191705,0.7325428987873788,0.7337298645028764,0.7349145951499599, 0.7360970871197343,0.7372773368101241,0.7384553406258837,0.7396310949786097,0.74080459628675,0.7419758409756163, 0.7431448254773942,0.744311546231154,0.7454759996828623,0.7466381822853914,0.7477980904985319,0.7489557207890021, 0.7501110696304595,0.7512641335035111,0.7524149088957244,0.7535633923016378,0.754709580222772,0.7558534691676396, 0.7569950556517564,0.7581343361976522,0.7592713073348808,0.7604059656000309,0.7615383075367367,0.7626683296956883, 0.7637960286346421,0.7649214009184317,0.7660444431189779,0.7671651518152995,0.7682835235935234,0.7693995550468951, 0.7705132427757893,0.77162458338772,0.7727335734973511,0.7738402097265061,0.7749444887041796,0.7760464070665459, 0.7771459614569709,0.778243148526021,0.7793379649314741,0.7804304073383297,0.7815204724188187,0.7826081568524139, 0.7836934573258397,0.7847763705330829,0.7858568931754019,0.7869350219613374,0.7880107536067219,0.7890840848346907, 0.7901550123756903,0.79122353296749,0.7922896433551907,0.7933533402912352,0.7944146205354181,0.7954734808548958, 0.7965299180241963,0.7975839288252284,0.7986355100472928,0.7996846584870905,0.8007313709487335,0.801775644243754, 0.8028174751911145,0.8038568606172174,0.8048937973559142,0.8059282822485158,0.8069603121438019,0.8079898838980305, 0.8090169943749475,0.810041640445796,0.8110638189893266,0.8120835268918062,0.8131007610470277,0.8141155183563192, 0.8151277957285542,0.8161375900801602,0.8171448983351285,0.8181497174250234,0.8191520442889918,0.820151875873772, 0.821149209133704,0.8221440410307373,0.8231363685344418,0.8241261886220157,0.8251134982782952,0.8260982944957639, 0.8270805742745618,0.8280603346224944,0.8290375725550416,0.8300122850953675,0.8309844692743282,0.8319541221304826, 0.8329212407100994,0.8338858220671681,0.8348478632634065,0.8358073613682702,0.8367643134589617,0.8377187166204387, 0.838670567945424,0.8396198645344132,0.8405666034956842,0.8415107819453062,0.8424523970071476,0.8433914458128856, 0.8443279255020151,0.8452618332218561,0.846193166127564,0.8471219213821372,0.8480480961564258,0.8489716876291414, 0.8498926929868639,0.8508111094240512,0.8517269341430476,0.8526401643540922,0.8535507972753273,0.8544588301328074, 0.8553642601605067,0.8562670846003282,0.8571673007021123,0.8580649057236446,0.8589598969306644,0.8598522715968734, 0.8607420270039435,0.8616291604415257,0.8625136692072574,0.8633955506067716,0.8642748019537047,0.8651514205697045, 0.8660254037844386,0.8668967489356028,0.8677654533689284,0.8686315144381913,0.869494929505219,0.8703556959398997, 0.8712138111201894,0.8720692724321206,0.8729220772698096,0.8737722230354652,0.8746197071393957,0.8754645270000179, 0.8763066800438636,0.8771461637055887,0.8779829754279805,0.8788171126619653,0.8796485728666165,0.8804773535091619, 0.8813034520649922,0.8821268660176678,0.8829475928589269,0.8837656300886935,0.8845809752150839,0.8853936257544159, 0.8862035792312147,0.8870108331782217,0.8878153851364013,0.8886172326549489,0.8894163732912975,0.8902128046111265, 0.8910065241883678,0.891797529605214,0.8925858184521255,0.8933713883278375,0.8941542368393681,0.894934361602025, 0.8957117602394129,0.8964864303834404,0.8972583696743284,0.8980275757606155,0.898794046299167,0.8995577789551804, 0.9003187714021935,0.9010770213220917,0.9018325264051138,0.9025852843498605,0.9033352928633008,0.9040825496607783, 0.9048270524660196,0.9055687990111395,0.9063077870366499,0.9070440142914649,0.9077774785329086,0.9085081775267219, 0.9092361090470685,0.9099612708765432,0.910683660806177,0.9114032766354453,0.912120116172273,0.9128341772330428, 0.9135454576426009,0.9142539552342637,0.9149596678498249,0.915662593339561,0.9163627295622396,0.917060074385124, 0.917754625683981,0.9184463813430871,0.9191353392552345,0.9198214973217376,0.9205048534524403,0.9211854055657211, 0.9218631515885005,0.92253
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -