三维随机数组的应用

需求:
在三维空间中,随机生成POINTNUM/2组空间点,每组有2个点,各组之间完全独立,
每组两点间距为LEN,要求各个点在长度为MAXN的正方体中。


抽象模型:
每个点用一维数组储存,数组元素类型是有三个变量的结构体,
通过随机生成三个变量,模拟随机生成空间点,
对于点a(x1,y1,z1)和点b(x2,y2,z2),
要求满足(x1-x2)²+(y1-y2)²+(z1-z2)²=LEN²(1≤x1,y1,z1≤MAXN)。


通过公式可看出,任意的平方项都非负,而且都要小于LEN²,
所以每个平方项里的两个变量之差不能超过LEN½,
当今且当其中两个平方项为0时,第三个平方项才为LEN²。


具体实现:
首先随机生成POINTNUM/2个满足条件的数组codit[i](i=0,2,4,...,POINTNUM-2),
假设a,b,c,d,e为已知满足条件的变量,k值未知,
对于每一个数组codit[i],假设其值为(a,b,c),求另一个随机数组codit[i+1],
随机生成另一个数组 (d,e,k),其中0≤d<a+LEN,0≤d<b+LEN,
通过(a-d)²+(b-e)²+(c-k)²=LEN² 求出k,
从而实现随机生成两个点的效果。
所有数值保留6位小数点


C++实现:

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <ctime>
#include <cmath>
#define DataType double                 //数组元素数值的类型
#define POINTNUM 200                    //点的数目,偶数
#define MAXN 1000                       //数组元素数值的最大值,正整数
#define DECIMAL 1000000                 //取随机小数时用的常量
#define LEN 100                         //两点间距

using namespace std;

struct node
{
    DataType x, y, z;                    //点坐标
}codit[POINTNUM]={0};

DataType randomFirstCodit()
{
    DataType inte = (DataType)(rand()%MAXN+1);
    DataType decil = (DataType)((rand()%DECIMAL)/(DECIMAL*0.1));
    return (DataType)(inte+decil);
}

DataType randomSecondX(DataType length)
{
    int temp = (int)sqrt(length);
    if(temp*temp == length)    temp--;
    DataType inte = (DataType)temp;
    DataType decil = (DataType)((rand()%DECIMAL)/(DECIMAL*0.1));
    return (DataType)(inte+decil);
}

DataType randomSecondY(DataType x1, DataType x2)
{
    DataType var = LEN*LEN - (x2-x1)*(x2-x1);
    return randomSecondX(var);
}

DataType caculate(DataType a,DataType b,DataType c,DataType d,DataType e)
{
    DataType sqrtNum = (DataType)sqrt(LEN*LEN - (d-a)*(d-a) - (e-b)*(e-b));
    return c-sqrtNum>0 ? c-sqrtNum : c+sqrtNum;
}
void print()
{
    for(int i = 0; i < POINTNUM-1; i+=2){
        if(i%2==0) printf("第%d组:\n",i/2+1);
        printf("(%.6lf , %.6lf , %.6lf)\n",codit[i].x,codit[i].y,codit[i].z);
        printf("(%.6lf , %.6lf , %.6lf)\n\n",codit[i+1].x,codit[i+1].y,codit[i+1].z);
    }
}
int main()
{
    srand((unsigned)time(NULL));
    for(int i = 0; i < POINTNUM-1; i+=2){
        codit[i].x = randomFirstCodit();
        codit[i].y = randomFirstCodit();
        codit[i].z = randomFirstCodit();

        DataType tempX = codit[i].x - randomSecondX(LEN);
        codit[i+1].x = tempX > 0 ? tempX : -tempX;

        DataType tempY = codit[i].y - randomSecondY(codit[i].x, codit[i+1].x);
        codit[i+1].y = tempY > 0 ? tempY : -tempY;
        codit[i+1].z = caculate(codit[i].x, codit[i].y,
                                 codit[i].z, codit[i+1].x, codit[i+1].y);
    }
    print();
    return 0;
}

测试例子:

第1组:
(24.232020 , 38.161630 , 74.195600)
(21.231350 , 29.143140 , 71.087093)

第2组:
(77.213400 , 95.210570 , 49.276390)
(74.103050 , 85.891430 , 47.411088)

第3组:
(84.131220 , 76.209620 , 62.182500)
(80.819140 , 67.167550 , 59.486003)

第4组:
(39.030220 , 20.297560 , 84.100440)
(35.864810 , 11.146040 , 81.604471)

第5组:
(19.194200 , 77.225330 , 20.085220)
(15.873550 , 68.042690 , 17.928276)

第6组:
(36.251290 , 45.151000 , 49.005340)
(33.188730 , 36.111120 , 46.021836)

第7组:
(70.286350 , 48.164810 , 53.184050)
(66.984670 , 38.966810 , 51.063742)

第8组:
(58.028090 , 8.182960 , 86.115540)
(54.811170 , 1.122580 , 79.806621)

第9组:
(11.043340 , 81.210020 , 26.171130)
(8.042170 , 72.086930 , 23.385056)

第10组:
(30.023140 , 86.314590 , 31.069730)
(26.815980 , 77.139800 , 28.716572)

第11组:
(83.058890 , 30.318290 , 24.010590)
(79.743680 , 21.171210 , 21.699679)

第12组:
(74.321060 , 59.181000 , 38.208680)
(71.193940 , 49.903550 , 36.171515)

第13组:
(18.123530 , 90.099440 , 87.143350)
(15.069390 , 80.936630 , 84.551991)

第14组:
(69.305660 , 5.319020 , 78.141650)
(66.006540 , 3.911170 , 68.807103)

第15组:
(78.084750 , 76.003590 , 1.053550)
(74.841420 , 66.704290 , 2.786706)

第16组:
(18.175460 , 61.078310 , 2.261870)
(15.171230 , 51.926190 , 4.947629)

第17组:
(4.051810 , 76.215270 , 63.111710)
(0.819830 , 67.071800 , 60.672189)

第18组:
(4.165480 , 23.119880 , 35.313000)
(1.020880 , 13.905730 , 33.030254)

第19组:
(24.272830 , 83.202860 , 42.141160)
(21.244970 , 73.947150 , 39.868739)

第20组:
(30.096650 , 69.327140 , 4.249250)
(27.088950 , 60.138630 , 1.694837)

第21组:
(8.028900 , 87.234080 , 7.278210)
(5.018310 , 78.153240 , 4.367077)

第22组:
(68.235980 , 64.010690 , 40.112520)
(64.979660 , 54.701850 , 38.456658)

第23组:
(32.132640 , 11.270110 , 94.193840)
(28.815430 , 2.151010 , 91.777617)

第24组:
(99.190640 , 45.079240 , 32.237160)
(96.170150 , 35.914930 , 29.611886)

第25组:
(46.183850 , 2.170340 , 66.281640)
(42.956030 , 6.994420 , 58.138580)

第26组:
(68.095790 , 24.295230 , 95.144110)
(64.880630 , 15.080090 , 92.966051)

第27组:
(94.085060 , 28.224930 , 6.224140)
(90.912480 , 19.058570 , 3.792560)

第28组:
(52.164050 , 83.135020 , 32.056390)
(48.859620 , 74.103740 , 29.314726)

第29组:
(50.246320 , 6.048790 , 16.180660)
(47.112010 , 3.054990 , 7.168820)

第30组:
(56.234910 , 66.304430 , 27.120830)
(52.926360 , 57.168750 , 24.755910)

第31组:
(10.151610 , 1.097170 , 49.009100)
(7.134520 , 8.095910 , 42.534935)

第32组:
(69.307220 , 48.322070 , 14.285250)
(66.117680 , 39.079140 , 12.188805)

第33组:
(61.202010 , 19.232040 , 29.126060)
(58.187080 , 10.123810 , 26.306425)

第34组:
(33.088620 , 92.034880 , 29.175550)
(29.868830 , 82.915380 , 26.632388)

第35组:
(97.282130 , 72.254580 , 91.073280)
(93.957080 , 63.085630 , 88.865476)

第36组:
(35.183410 , 12.150570 , 49.283520)
(32.143370 , 3.034420 , 46.516940)

第37组:
(98.292580 , 1.109950 , 2.033120)
(95.086800 , 8.124870 , 8.398171)

第38组:
(5.003560 , 4.148710 , 84.205620)
(1.992540 , 4.913340 , 74.700406)

第39组:
(78.027100 , 47.092130 , 96.050360)
(74.715820 , 37.854410 , 94.126834)

第40组:
(49.267280 , 10.123930 , 38.158470)
(45.995900 , 0.998990 , 35.702143)

第41组:
(25.032690 , 43.065640 , 30.236960)
(21.921610 , 34.006640 , 27.363687)

第42组:
(30.254690 , 11.007630 , 40.200580)
(27.131400 , 1.974780 , 37.259036)

第43组:
(90.176040 , 7.325740 , 21.321540)
(86.951240 , 1.736150 , 13.682355)

第44组:
(87.306420 , 88.100490 , 40.109540)
(84.266940 , 78.838480 , 37.878681)

第45组:
(18.176010 , 43.164790 , 2.181830)
(15.006690 , 33.851470 , 0.388096)

第46组:
(8.062260 , 89.294260 , 70.242100)
(4.996460 , 80.018770 , 68.105244)

第47组:
(80.136080 , 32.043160 , 53.049460)
(77.097900 , 22.909810 , 50.338117)

第48组:
(6.304420 , 56.276490 , 43.100650)
(3.114570 , 47.237690 , 40.250220)

第49组:
(30.093820 , 67.155630 , 33.059470)
(26.879000 , 57.889600 , 31.108670)

第50组:
(46.213210 , 86.063080 , 45.176340)
(43.158910 , 76.838460 , 42.814638)

第51组:
(8.225680 , 41.308480 , 5.127330)
(5.122830 , 32.173400 , 2.496240)

第52组:
(5.193220 , 27.203050 , 31.187880)
(1.884710 , 18.079170 , 28.777781)

第53组:
(29.068750 , 52.124320 , 35.153810)
(25.935650 , 43.109520 , 32.167664)

第54组:
(68.112110 , 81.017230 , 61.269300)
(65.084020 , 71.872910 , 58.583768)

第55组:
(46.036290 , 24.229930 , 51.229770)
(42.870180 , 14.978130 , 49.136938)

第56组:
(28.160730 , 89.096870 , 17.191790)
(25.128090 , 80.047700 , 14.205887)

第57组:
(53.311780 , 38.167270 , 80.185570)
(50.123230 , 29.008190 , 77.747455)

第58组:
(74.074890 , 31.214680 , 78.223780)
(70.769580 , 22.069630 , 75.890759)

第59组:
(75.070720 , 35.236720 , 66.123590)
(72.044300 , 26.049780 , 63.585694)

第60组:
(31.323190 , 94.184940 , 40.025100)
(28.289500 , 85.008920 , 37.456563)

第61组:
(28.306820 , 100.063440 , 5.295320)
(25.265400 , 90.901770 , 2.685039)

第62组:
(77.018090 , 24.068330 , 17.162430)
(73.745800 , 14.884610 , 14.937255)

第63组:
(22.104710 , 3.112920 , 43.323120)
(18.915870 , 6.032790 , 34.306155)

第64组:
(15.069800 , 33.049080 , 1.020370)
(12.018540 , 23.992240 , 3.963746)

第65组:
(85.121470 , 52.257380 , 37.060400)
(82.062960 , 42.942580 , 35.090624)

第66组:
(100.114290 , 52.312020 , 25.300030)
(97.092380 , 43.248430 , 22.347168)

第67组:
(59.170040 , 60.308220 , 12.124100)
(55.943490 , 51.128510 , 9.817089)

第68组:
(27.028800 , 35.264420 , 63.274640)
(23.824970 , 26.190960 , 60.552913)

第69组:
(69.079330 , 8.220860 , 32.133390)
(66.076870 , 0.880900 , 26.041574)

第70组:
(75.136670 , 79.051430 , 14.288190)
(72.110750 , 69.961720 , 11.420965)

第71组:
(39.226770 , 5.299490 , 92.225190)
(36.135130 , 3.887440 , 82.820517)

第72组:
(56.101390 , 28.105590 , 82.321760)
(53.095680 , 18.979140 , 79.551630)

第73组:
(34.104640 , 53.091230 , 79.275950)
(31.089850 , 43.928290 , 76.639367)

第74组:
(19.114450 , 1.040870 , 21.198350)
(15.962510 , 8.044960 , 14.794601)

第75组:
(85.163320 , 76.229230 , 67.106450)
(82.048260 , 66.989780 , 64.886323)

第76组:
(5.117090 , 48.168190 , 78.115390)
(2.111610 , 38.877740 , 75.957931)

第77组:
(82.049970 , 11.016100 , 21.014260)
(78.761750 , 2.013990 , 18.159505)

第78组:
(19.005140 , 21.061860 , 13.194120)
(15.686390 , 12.012060 , 10.531975)

第79组:
(41.124080 , 79.250130 , 81.323180)
(38.100450 , 69.994360 , 79.045378)

第80组:
(86.161430 , 31.075040 , 100.029070)
(83.013910 , 21.916230 , 97.537220)

第81组:
(93.023860 , 6.153300 , 2.277350)
(89.940240 , 3.126850 , 11.295769)

第82组:
(47.022250 , 44.233850 , 51.203560)
(43.853570 , 35.080320 , 48.719137)

第83组:
(55.181760 , 91.197490 , 7.212090)
(51.931070 , 82.196750 , 4.310419)

第84组:
(23.248230 , 49.114510 , 87.200780)
(20.148470 , 39.960180 , 84.633733)

第85组:
(22.063200 , 33.015560 , 91.120340)
(19.039400 , 23.821950 , 88.603563)

第86组:
(47.307640 , 78.031160 , 57.246930)
(44.257030 , 68.943490 , 54.399469)

第87组:
(92.009740 , 92.244510 , 45.185100)
(88.711970 , 83.175800 , 42.561512)

第88组:
(89.319660 , 13.257590 , 43.205340)
(86.082980 , 4.039050 , 41.074044)

第89组:
(5.133200 , 89.108880 , 86.001220)
(2.005800 , 80.012710 , 83.266433)

第90组:
(48.276100 , 35.249060 , 49.108930)
(45.239160 , 26.204450 , 46.113596)

第91组:
(92.192730 , 98.246940 , 89.090930)
(89.147460 , 89.001930 , 86.798307)

第92组:
(95.246210 , 68.157780 , 1.037370)
(92.245300 , 59.018420 , 3.769885)

第93组:
(86.127360 , 43.294130 , 46.142490)
(82.990780 , 34.106210 , 43.745827)

第94组:
(85.253790 , 62.054680 , 38.321820)
(82.002530 , 52.991520 , 35.622109)

第95组:
(73.260730 , 60.070790 , 97.198130)
(70.180580 , 50.909360 , 94.632808)

第96组:
(69.154340 , 30.134200 , 52.111370)
(65.957310 , 20.927740 , 49.870813)

第97组:
(26.195220 , 97.114750 , 92.203420)
(23.022840 , 87.927090 , 89.853333)

第98组:
(7.167510 , 9.263770 , 33.199270)
(3.954490 , 0.235250 , 30.342291)

第99组:
(33.327420 , 36.247660 , 20.212080)
(30.269900 , 26.968510 , 18.079254)

第100组:
(80.098870 , 19.188540 , 84.278610)
(77.056490 , 9.960290 , 81.915704)



猜你喜欢

转载自blog.csdn.net/gnosed/article/details/79702501
今日推荐