请选择 进入手机版 | 继续访问电脑版
 找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 810|回复: 0

两条直角边算角度

[复制链接]

821

主题

821

帖子

2657

积分

管理员

Rank: 9Rank: 9Rank: 9

积分
2657
发表于 2019-8-30 08:45:51 | 显示全部楼层 |阅读模式
一个非常基本的程序,没什么看头,基本就是一大堆定理算出来,写的在里是发现CSS3出来后,算角度的需求越来越多,每次算还要用到笔纸,与是乎总结下来搞这样一个小工具,由于需求一般都是直角的,所以这里只算直角,根据两边长度求角度。
函数中的sins是拷贝的一份正弦表,我把它倒了方向写成了一个对象,没什么看头,可以看源码(一个很长的json),这里的思路是取这个正弦值最附近的一位在sins中有key的,就返回它的角度。
  1. var sins={
  2. 0017:0.1,0035:0.2,0052:0.3,0070:0.4,0087:0.5,0105:0.6,0122:0.7,0140:0.8,0157:0.9,0175:1.0,0192:1.1,0209:1.2,0227:1.3,0244:1.4,0262:1.5,0279:1.6,0297:1.7,0314:1.8,0332:1.9,0349:2.0,0366:2.1,0384:2.2,0401:2.3,0419:2.4,0436:2.5,0454:2.6,0471:2.7,0488:2.8,0506:2.9,0523:3.0,0541:3.1,0558:3.2,0576:3.3,0593:3.4,0610:3.5,0628:3.6,0645:3.7,0663:3.8,0680:3.9,0698:4.0,0715:4.1,0732:4.2,0750:4.3,0767:4.4,0785:4.5,0802:4.6,0819:4.7,0837:4.8,0854:4.9,0872:5.0,0889:5.1,0906:5.2,0924:5.3,0941:5.4,0958:5.5,0976:5.6,0993:5.7,1011:5.8,1028:5.9,1045:6.0,1063:6.1,1080:6.2,1097:6.3,1115:6.4,1132:6.5,1149:6.6,1167:6.7,1184:6.8,1201:6.9,1219:7.0,1236:7.1,1253:7.2,1271:7.3,1288:7.4,1305:7.5,1323:7.6,1340:7.7,1357:7.8,1374:7.9,1392:8.0,1409:8.1,1426:8.2,1444:8.3,1461:8.4,1478:8.5,1495:8.6,1513:8.7,1530:8.8,1547:8.9,1564:9.0,1582:9.1,1599:9.2,1616:9.3,1633:9.4,1650:9.5,1668:9.6,1685:9.7,1702:9.8,1719:9.9,1736:10.0,1754:10.1,1771:10.2,1788:10.3,1805:10.4,1822:10.5,1840:10.6,1857:10.7,1874:10.8,1891:10.9,1908:11.0,1925:11.1,1942:11.2,1959:11.3,1977:11.4,1994:11.5,2011:11.6,2028:11.7,2045:11.8,2062:11.9,2079:12.0,2096:12.1,2113:12.2,2130:12.3,2147:12.4,2164:12.5,2181:12.6,2198:12.7,2215:12.8,2233:12.9,2250:13.0,2267:13.1,2284:13.2,2300:13.3,2317:13.4,2334:13.5,2351:13.6,2368:13.7,2385:13.8,2402:13.9,2419:14.0,2436:14.1,2453:14.2,2470:14.3,2487:14.4,2504:14.5,2521:14.6,2538:14.7,2554:14.8,2571:14.9,2588:15.0,2605:15.1,2622:15.2,2639:15.3,2656:15.4,2672:15.5,2689:15.6,2706:15.7,2723:15.8,2740:15.9,2756:16.0,2773:16.1,2790:16.2,2807:16.3,2823:16.4,2840:16.5,2857:16.6,2874:16.7,2890:16.8,2907:16.9,2924:17.0,2940:17.1,2957:17.2,2974:17.3,2990:17.4,3007:17.5,3024:17.6,3040:17.7,3057:17.8,3074:17.9,3090:18.0,3107:18.1,3123:18.2,3140:18.3,3156:18.4,3173:18.5,3190:18.6,3206:18.7,3223:18.8,3239:18.9,3256:19.0,3272:19.1,3289:19.2,3305:19.3,3322:19.4,3338:19.5,3355:19.6,3371:19.7,3387:19.8,3404:19.9,3420:20.0,3437:20.1,3453:20.2,3469:20.3,3486:20.4,3502:20.5,3518:20.6,3535:20.7,3551:20.8,3567:20.9,3584:21.0,3600:21.1,3616:21.2,3633:21.3,3649:21.4,3665:21.5,3681:21.6,3697:21.7,3714:21.8,3730:21.9,3746:22.0,3762:22.1,3778:22.2,3795:22.3,3811:22.4,3827:22.5,3843:22.6,3859:22.7,3875:22.8,3891:22.9,3907:23.0,3923:23.1,3939:23.2,3955:23.3,3971:23.4,3987:23.5,4003:23.6,4019:23.7,4035:23.8,4051:23.9,4067:24.0,4083:24.1,4099:24.2,4115:24.3,4131:24.4,4147:24.5,4163:24.6,4179:24.7,4195:24.8,4210:24.9,4226:25.0,4242:25.1,4258:25.2,4274:25.3,4289:25.4,4305:25.5,4321:25.6,4337:25.7,4352:25.8,4368:25.9,4384:26.0,4399:26.1,4415:26.2,4431:26.3,4446:26.4,4462:26.5,4478:26.6,4493:26.7,4509:26.8,4524:26.9,4540:27.0,4555:27.1,4571:27.2,4586:27.3,4602:27.4,4617:27.5,4633:27.6,4648:27.7,4664:27.8,4679:27.9,4695:28.0,4710:28.1,4726:28.2,4741:28.3,4756:28.4,4772:28.5,4787:28.6,4802:28.7,4818:28.8,4833:28.9,4848:29.0,4863:29.1,4879:29.2,4894:29.3,4909:29.4,4924:29.5,4939:29.6,4955:29.7,4970:29.8,4985:29.9,5000:30.0,5015:30.1,5030:30.2,5045:30.3,5060:30.4,5075:30.5,5090:30.6,5105:30.7,5120:30.8,5135:30.9,5150:31.0,5165:31.1,5180:31.2,5195:31.3,5210:31.4,5225:31.5,5240:31.6,5255:31.7,5270:31.8,5284:31.9,5299:32.0,5314:32.1,5329:32.2,5344:32.3,5358:32.4,5373:32.5,5388:32.6,5402:32.7,5417:32.8,5432:32.9,5446:33.0,5461:33.1,5476:33.2,5490:33.3,5505:33.4,5519:33.5,5534:33.6,5548:33.7,5563:33.8,5577:33.9,5592:34.0,5606:34.1,5621:34.2,5635:34.3,5650:34.4,5664:34.5,5678:34.6,5693:34.7,5707:34.8,5721:34.9,5736:35.0,5750:35.1,5764:35.2,5779:35.3,5793:35.4,5807:35.5,5821:35.6,5835:35.7,5850:35.8,5864:35.9,5878:36.0,5892:36.1,5906:36.2,5920:36.3,5934:36.4,5948:36.5,5962:36.6,5976:36.7,5990:36.8,6004:36.9,6018:37.0,6032:37.1,6046:37.2,6060:37.3,6074:37.4,6088:37.5,6101:37.6,6115:37.7,6129:37.8,6143:37.9,6157:38.0,6170:38.1,6184:38.2,6198:38.3,6211:38.4,6225:38.5,6239:38.6,6252:38.7,6266:38.8,6280:38.9,6293:39.0,6307:39.1,6320:39.2,6334:39.3,6347:39.4,6361:39.5,6374:39.6,6388:39.7,6401:39.8,6414:39.9,6428:40.0,6441:40.1,6455:40.2,6468:40.3,6481:40.4,6494:40.5,6508:40.6,6521:40.7,6534:40.8,6547:40.9,6561:41.0,6574:41.1,6587:41.2,6600:41.3,6613:41.4,6626:41.5,6639:41.6,6652:41.7,6665:41.8,6678:41.9,6691:42.0,6704:42.1,6717:42.2,6730:42.3,6743:42.4,6756:42.5,6769:42.6,6782:42.7,6794:42.8,6807:42.9,6820:43.0,6833:43.1,6845:43.2,6858:43.3,6871:43.4,6884:43.5,6896:43.6,6909:43.7,6921:43.8,6934:43.9,6947:44.0,6959:44.1,6972:44.2,6984:44.3,6997:44.4,7009:44.5,7022:44.6,7034:44.7,7046:44.8,7059:44.9,7071:45.0,7083:45.1,7096:45.2,7108:45.3,7120:45.4,7133:45.5,7145:45.6,7157:45.7,7169:45.8,7181:45.9,7193:46.0,7206:46.1,7218:46.2,7230:46.3,7242:46.4,7254:46.5,7266:46.6,7278:46.7,7290:46.8,7302:46.9,7314:47.0,7325:47.1,7337:47.2,7349:47.3,7361:47.4,7373:47.5,7385:47.6,7396:47.7,7408:47.8,7420:47.9,7431:48.0,7443:48.1,7455:48.2,7466:48.3,7478:48.4,7490:48.5,7501:48.6,7513:48.7,7524:48.8,7536:48.9,7547:49.0,7559:49.1,7570:49.2,7581:49.3,7593:49.4,7604:49.5,7615:49.6,7627:49.7,7638:49.8,7649:49.9,7660:50.0,7672:50.1,7683:50.2,7694:50.3,7705:50.4,7716:50.5,7727:50.6,7738:50.7,7749:50.8,7760:50.9,7771:51.0,7782:51.1,7793:51.2,7804:51.3,7815:51.4,7826:51.5,7837:51.6,7848:51.7,7859:51.8,7869:51.9,7880:52.0,7891:52.1,7902:52.2,7912:52.3,7923:52.4,7934:52.5,7944:52.6,7955:52.7,7965:52.8,7976:52.9,7986:53.0,7997:53.1,8007:53.2,8018:53.3,8028:53.4,8039:53.5,8049:53.6,8059:53.7,8070:53.8,8080:53.9,8090:54.0,8100:54.1,8111:54.2,8121:54.3,8131:54.4,8141:54.5,8151:54.6,8161:54.7,8171:54.8,8181:54.9,8192:55.0,8202:55.1,8211:55.2,8221:55.3,8231:55.4,8241:55.5,8251:55.6,8261:55.7,8271:55.8,8281:55.9,8290:56.0,8300:56.1,8310:56.2,8320:56.3,8329:56.4,8339:56.5,8348:56.6,8358:56.7,8368:56.8,8377:56.9,8387:57.0,8396:57.1,8406:57.2,8415:57.3,8425:57.4,8434:57.5,8443:57.6,8453:57.7,8462:57.8,8471:57.9,8480:58.0,8490:58.1,8499:58.2,8508:58.3,8517:58.4,8526:58.5,8536:58.6,8545:58.7,8554:58.8,8563:58.9,8572:59.0,8581:59.1,8590:59.2,8599:59.3,8607:59.4,8616:59.5,8625:59.6,8634:59.7,8643:59.8,8652:59.9,8660:60.0,8669:60.1,8678:60.2,8686:60.3,8695:60.4,8704:60.5,8712:60.6,8721:60.7,8729:60.8,8738:60.9,8746:61.0,8755:61.1,8763:61.2,8771:61.3,8780:61.4,8788:61.5,8796:61.6,8805:61.7,8813:61.8,8821:61.9,8829:62.0,8838:62.1,8846:62.2,8854:62.3,8862:62.4,8870:62.5,8878:62.6,8886:62.7,8894:62.8,8902:62.9,8910:63.0,8918:63.1,8926:63.2,8934:63.3,8942:63.4,8949:63.5,8957:63.6,8965:63.7,8973:63.8,8980:63.9,8988:64.0,8996:64.1,9003:64.2,9011:64.3,9018:64.4,9026:64.5,9033:64.6,9041:64.7,9048:64.8,9056:64.9,9063:65.0,9070:65.1,9078:65.2,9085:65.3,9092:65.4,9100:65.5,9107:65.6,9114:65.7,9121:65.8,9128:65.9,9135:66.0,9143:66.1,9150:66.2,9157:66.3,9164:66.4,9171:66.5,9178:66.6,9184:66.7,9191:66.8,9198:66.9,9205:67.0,9212:67.1,9219:67.2,9225:67.3,9232:67.4,9239:67.5,9245:67.6,9252:67.7,9259:67.8,9265:67.9,9272:68.0,9278:68.1,9285:68.2,9291:68.3,9298:68.4,9304:68.5,9311:68.6,9317:68.7,9323:68.8,9330:68.9,9336:69.0,9342:69.1,9348:69.2,9354:69.3,9361:69.4,9367:69.5,9373:69.6,9379:69.7,9385:69.8,9391:69.9,9397:70.0,9403:70.1,9409:70.2,9415:70.3,9421:70.4,9426:70.5,9432:70.6,9438:70.7,9444:70.8,9449:70.9,9455:71.0,9461:71.1,9466:71.2,9472:71.3,9478:71.4,9483:71.5,9489:71.6,9494:71.7,9500:71.8,9505:71.9,9511:72.0,9516:72.1,9521:72.2,9527:72.3,9532:72.4,9537:72.5,9542:72.6,9548:72.7,9553:72.8,9558:72.9,9563:73.0,9568:73.1,9573:73.2,9578:73.3,9583:73.4,9588:73.5,9593:73.6,9598:73.7,9603:73.8,9608:73.9,9613:74.0,9617:74.1,9622:74.2,9627:74.3,9632:74.4,9636:74.5,9641:74.6,9646:74.7,9650:74.8,9655:74.9,9659:75.0,9664:75.1,9668:75.2,9673:75.3,9677:75.4,9681:75.5,9686:75.6,9690:75.7,9694:75.8,9699:75.9,9703:76.0,9707:76.1,9711:76.2,9715:76.3,9720:76.4,9724:76.5,9728:76.6,9732:76.7,9736:76.8,9740:76.9,9744:77.0,9748:77.1,9751:77.2,9755:77.3,9759:77.4,9763:77.5,9767:77.6,9770:77.7,9774:77.8,9778:77.9,9781:78.0,9785:78.1,9789:78.2,9792:78.3,9796:78.4,9799:78.5,9803:78.6,9806:78.7,9810:78.8,9813:78.9,9816:79.0,9820:79.1,9823:79.2,9826:79.3,9829:79.4,9833:79.5,9836:79.6,9839:79.7,9842:79.8,9845:79.9,9848:80.0,9851:80.1,9854:80.2,9857:80.3,9860:80.4,9863:80.5,9866:80.6,9869:80.7,9871:80.8,9874:80.9,9877:81.0,9880:81.1,9882:81.2,9885:81.3,9888:81.4,9890:81.5,9893:81.6,9895:81.7,9898:81.8,9900:81.9,9903:82.0,9905:82.1,9907:82.2,9910:82.3,9912:82.4,9914:82.5,9917:82.6,9919:82.7,9921:82.8,9923:82.9,9925:83.0,9928:83.1,9930:83.2,9932:83.3,9934:83.4,9936:83.5,9938:83.6,9940:83.7,9942:83.8,9943:83.9,9945:84.0,9947:84.1,9949:84.2,9951:84.3,9952:84.4,9954:84.5,9956:84.6,9957:84.7,9959:84.8,9960:84.9,9962:85.0,9963:85.1,9965:85.2,9966:85.3,9968:85.4,9969:85.5,9971:85.6,9972:85.7,9973:85.8,9974:85.9,9976:86.0,9977:86.1,9978:86.2,9979:86.3,9980:86.4,9981:86.5,9982:86.6,9983:86.7,9984:86.8,9985:86.9,9986:87.0,9987:87.1,9988:87.2,9989:87.3,9990:87.4,9990:87.5,9991:87.6,9992:87.7,9993:87.8,9994:88.0,9995:88.1,9996:88.3,9997:88.5,9998:88.8,9999:89.1
  3. };
  4.     function toMath(x,y){
  5.         var z=Math.sqrt(x*x+y*y);
  6.         var skey=Math.floor(x/z*10000);
  7.         if(!sins.hasOwnProperty(skey)){
  8.         for(var i=1;i<=100;i++){
  9.             var akey = skey - i;
  10.             if(sins.hasOwnProperty(akey)){
  11.             skey = akey;
  12.             break;
  13.             }
  14.             var bkey = skey + i;
  15.             if(sins.hasOwnProperty(bkey)){
  16.             skey = bkey;
  17.             break;
  18.             }
  19.         }
  20.         }
  21.         return {'x':x,'y':y,'z':z,'a':sins[skey],'b':90-sins[skey]};
  22.     }
  23.     console.log(toMath(50,50))
复制代码


回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

用户反馈
客户端