-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhexagon_area.py
executable file
·1472 lines (1243 loc) · 51.5 KB
/
hexagon_area.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python
#First import the netcdf4 library
from netCDF4 import Dataset # http://code.google.com/p/netcdf4-python/
import numpy as np # http://code.google.com/p/netcdf4-python/
import matplotlib
import math
import os
matplotlib.use("GTKAgg")
from pylab import *
#import matplotlib.pyplot as plt
import pdb
import netCDF4 as nc
#######################
def Divide_hexagon_into_4_quadrants_old(x0,y0,H):
S=(2/sqrt(3))*H
Area_hex=3*(np.sqrt(3)/2)*(S**2) #Area of the hexagon (should be equal to Area/grid_area, since it is not dim) - check this
#print 'y0,H',y0, H
#Defining boundaries of hexagon, and if statements to see which side of the boundary you are on
W1= False ; W2=False ;W3=False ;W4=False ;W5=False ;W6=False ;H1=False ;V1=False ;V2=False
W1=(-y0<-sqrt(3)*(-x0) + ((sqrt(3)*(S))));#upper right
W2=(-y0<(H));#Top
W3=(-y0<sqrt(3)*(-x0) + ((sqrt(3)*(S))));#Upper left
W4=(-y0<sqrt(3)*(-x0) + (-(sqrt(3)*(S))));#Lower right
W5=(-y0>-H);#Bottom
W6=(-y0<-sqrt(3)*(-x0) + (-(sqrt(3)*(S))));#Lower left
T1=(-x0<S) #Right
T2=(-y0<(H));#Top
T3= (-x0>-S) #Left
T4=(-y0>-H);#Bottom
H1=(y0<0);
V1=(x0<-(S/2));
V2=(x0<(S/2));
#Deciding if the origin is within the hexagon
#print W1 , W2 , W3 , W4 , W5 , W6
#if In_hex:
# print In_hex
#Calculating the area of the top and bottom half of the hexagon, 2 Cases for the majority above and below the y0=0 line
#(and two more for the hexagon totally above and below the y0=0 line)
if abs(y0)<H:
Trapesium=((sqrt(3)*H)-(abs(y0)/sqrt(3)))*(H-abs(y0));
if y0>=0:
Area_Lower=Trapesium;
Area_Upper=Area_hex-Trapesium;
else:
Area_Upper=Trapesium;
Area_Lower=Area_hex-Trapesium;
else:
if y0>=0:
Area_Lower=0.;
Area_Upper=Area_hex;
else:
Area_Lower=Area_hex;
Area_Upper=0.;
#Calcularing Left and Right area of the hexagon, about the x0=0 line, 3 cases:
#(and two more for when the hexagon is totally to the left or right of the x0=0 line)
if abs(x0)<S:
if abs(x0)<S/2:
Rectangle=(abs(x0)*2*H);
Big_side =(Area_hex/2) +Rectangle;
Small_side=Area_hex-Big_side;
else:
Triangle=(sqrt(3))*((S-abs(x0))**2);
Small_side=Triangle;
Big_side=Area_hex-Small_side;
if x0>=0.:
Area_right=Big_side;
Area_left=Small_side;
else:
Area_right=Small_side;
Area_left=Big_side;
else:
if x0>=0.:
Area_right=Area_hex;
Area_left=0.;
else:
Area_right=0.;
Area_left=Area_hex;
In_hex= W1 & W2 & W3 & W4 & W5 & W6;
In_hex_box=T1 & T2 & T3 & T4
Area_Q1=0.; Area_Q2=0. ; Area_Q3=0.; Area_Q4=0.;
Sector=0
#if In_hex==False: #Then the hexagon is completely contained in the middle cell
if In_hex_box==False: #Then the hexagon is completely contained in the middle cell
Sector=-1
#mass_on_ocean[i,j,5]=mass_on_ocean[i,j,5]+Mass
if min(Area_Upper,Area_Lower)==0.:
Sector=-2
if Area_Upper==0.:
Area_Q3=Area_left;
Area_Q4=Area_right;
if Area_Lower==0.:
Area_Q1=Area_right;
Area_Q2=Area_left;
elif min(Area_right,Area_left)==0.:
Sector=-3
if Area_right==0.:
Area_Q2=Area_Upper;
Area_Q3=Area_Lower;
if Area_left==0.:
Area_Q1=Area_Upper;
Area_Q4=Area_Lower;
#yCxC=1.
#print 'out of hex'
else:
#Determine which sector within the hexagon you are in. (sectors 1 to 6 go counter clockwise starting with top right)
if (H1==True): #Bottom half
if V1:
#if W1==False:
if ((y0+(sqrt(3)*(x0+S)))<=0.):
Sector=1;
else:
Sector=2;
elif (V1==False) & (V2==True):
Sector=3;
else:
#if (W3==True):
if ((y0-(sqrt(3)*(x0-S)))>=0.):
Sector=4;
else:
Sector=5;
else: #Bottom half
if V1:
#if W6==False:
if ((y0 -(sqrt(3)*(x0+S)))>=0.):
Sector=10;
else:
Sector=9;
elif (V1==False) & (V2==True):
Sector=8;
else:
#if (W4==True):
if ((y0+(sqrt(3)*(x0-S)))<=0.):
Sector=7;
else:
Sector=6;
#print Sector
#If the hexagon is in Sector 1,3,4 or 6, then the intersetion of the hexagon and the corresponding sector forms a baby triangle
#If the hexagon is in Sector 2,5 then the intersetion of the hexagon and the corresponding sector forms a baby trapesoid
if Sector==2 or Sector==4 or Sector==7 or Sector==9:
Baby_triangle=(1/(2*sqrt(3)))*((-abs(y0)+(sqrt(3)*(S-abs(x0))))**2);
else:
#Baby_trap= (H-abs(y0)) * ((-H-abs(y0)+(2*sqrt(3)*(S-abs(x0))))/(2*sqrt(3)));
Baby_trap=(H-abs(y0))*((S-abs(x0) - ((H+abs(y0))/(2*sqrt(3))))) ;
#Finally, we assign the correct areas in each quadrant (Q1,Q2,Q3,Q4), depending on which sector you are in.
C1=0.;C2=0.;C3=0.;C4=0.;
#Corner cases
if Sector==2:
Area_Q1=Baby_triangle;
Area_Q2=Area_Upper-Area_Q1
Area_Q3=Area_left-Area_Q2
Area_Q4=Area_right-Area_Q1
if Sector==4:
Area_Q2=Baby_triangle;
Area_Q1=Area_Upper-Area_Q2
Area_Q3=Area_left-Area_Q2
Area_Q4=Area_right-Area_Q1
if Sector==7:
Area_Q3=Baby_triangle;
Area_Q2=Area_left-Area_Q3
Area_Q1=Area_Upper-Area_Q2
Area_Q4=Area_right-Area_Q1
if Sector==9:
Area_Q4=Baby_triangle;
Area_Q1=Area_right-Area_Q4
Area_Q2=Area_Upper-Area_Q1
Area_Q3=Area_left-Area_Q2
#Center cases
if Sector==3:
if x0<=0.:
Area_Q1=Baby_trap;
Area_Q2=Area_Upper-Area_Q1;
Area_Q3=Area_left-Area_Q2;
Area_Q4=Area_right-Area_Q1;
else:
Area_Q2=Baby_trap;
Area_Q1=Area_Upper-Area_Q2;
Area_Q3=Area_left-Area_Q2;
Area_Q4=Area_right-Area_Q1;
if Sector==8:
if x0<=0.:
Area_Q4=Baby_trap;
Area_Q3=Area_Lower-Area_Q4;
Area_Q1=Area_right-Area_Q4;
Area_Q2=Area_Upper-Area_Q1;
else:
Area_Q3=Baby_trap;
Area_Q4=Area_Lower-Area_Q3;
Area_Q1=Area_right-Area_Q4;
Area_Q2=Area_Upper-Area_Q1;
#Outside triangle cases:
if Sector==1:
Area_Q1=0.;
Area_Q2=Area_Upper;
Area_Q4=Area_right;
Area_Q3=Area_left-Area_Q2;
if Sector==5:
Area_Q2=0.;
Area_Q1=Area_Upper;
Area_Q3=Area_left;
Area_Q4=Area_Lower-Area_Q3;
if Sector==6:
Area_Q3=0.;
Area_Q2=Area_left;
Area_Q4=Area_Lower;
Area_Q1=Area_right-Area_Q4;
if Sector==10:
Area_Q4=0.;
Area_Q3=Area_Lower;
Area_Q1=Area_right;
Area_Q2=Area_left-Area_Q3;
#print x0,y0,Sector
return [Area_hex, Area_Q1, Area_Q2, Area_Q3, Area_Q4]
def Hexagon_into_quadrants_using_triangles_across_lines(x0,y0,H,theta, Px, Py, Qx, Qy, Rx, Ry, Sx, Sy ):
#In this routine, the hexagon is divided across lines PQ, RS.
#Length of side of Hexagon
S=(2/sqrt(3))*H;
#Finding positions of corners
C1x=S ; C1y=0. #Corner 1 (right)
C2x=H/sqrt(3.) ; C2y=H; #Corner 2 (top right)
C3x=-H/sqrt(3.) ; C3y=H; #Corner 3 (top left)
C4x=-S ; C4y=0.; #Corner 4 (left)
C5x=-H/sqrt(3.) ; C5y=-H; #Corner 5 (bottom right)
C6x=H/sqrt(3.) ; C6y=-H; #Corner 6 (bottom right)
#Finding positions of corners
[C1x,C1y]=rotate_and_translate(C1x,C1y,theta,x0,y0)
[C2x,C2y]=rotate_and_translate(C2x,C2y,theta,x0,y0)
[C3x,C3y]=rotate_and_translate(C3x,C3y,theta,x0,y0)
[C4x,C4y]=rotate_and_translate(C4x,C4y,theta,x0,y0)
[C5x,C5y]=rotate_and_translate(C5x,C5y,theta,x0,y0)
[C6x,C6y]=rotate_and_translate(C6x,C6y,theta,x0,y0)
#Area of Hexagon is the sum of the triangles
[T12_Area,T12_Q1,T12_Q2,T12_Q3,T12_Q4]=Triangle_divided_into_four_parts(x0,y0,C1x,C1y,C2x,C2y, Px, Py, Qx, Qy, Rx, Ry, Sx, Sy ); #T012
[T23_Area,T23_Q1,T23_Q2,T23_Q3,T23_Q4]=Triangle_divided_into_four_parts(x0,y0,C2x,C2y,C3x,C3y, Px, Py, Qx, Qy, Rx, Ry, Sx, Sy ); #T023
[T34_Area,T34_Q1,T34_Q2,T34_Q3,T34_Q4]=Triangle_divided_into_four_parts(x0,y0,C3x,C3y,C4x,C4y, Px, Py, Qx, Qy, Rx, Ry, Sx, Sy ); #T034
[T45_Area,T45_Q1,T45_Q2,T45_Q3,T45_Q4]=Triangle_divided_into_four_parts(x0,y0,C4x,C4y,C5x,C5y, Px, Py, Qx, Qy, Rx, Ry, Sx, Sy ); #T023
[T56_Area,T56_Q1,T56_Q2,T56_Q3,T56_Q4]=Triangle_divided_into_four_parts(x0,y0,C5x,C5y,C6x,C6y, Px, Py, Qx, Qy, Rx, Ry, Sx, Sy ); #T023
[T61_Area,T61_Q1,T61_Q2,T61_Q3,T61_Q4]=Triangle_divided_into_four_parts(x0,y0,C6x,C6y,C1x,C1y, Px, Py, Qx, Qy, Rx, Ry, Sx, Sy ); #T023
#Area of Hexagon is the sum of the triangles
#print '1:', Triangle_divided_into_four_parts(x0,y0,C1x,C1y,C2x,C2y, Px, Py, Qx, Qy, Rx, Ry, Sx, Sy ); #T012
#print '1b:', Triangle_divided_into_four_quadrants(x0,y0,C1x,C1y,C2x,C2y); #T012
#print '2', Triangle_divided_into_four_parts(x0,y0,C2x,C2y,C3x,C3y, Px, Py, Qx, Qy, Rx, Ry, Sx, Sy ); #T023
#print '2b',Triangle_divided_into_four_quadrants(x0,y0,C2x,C2y,C3x,C3y); #T023
#print '3:',Triangle_divided_into_four_parts(x0,y0,C3x,C3y,C4x,C4y, Px, Py, Qx, Qy, Rx, Ry, Sx, Sy ); #T034
#print '3b',Triangle_divided_into_four_quadrants(x0,y0,C3x,C3y,C4x,C4y); #T034
#print '4',Triangle_divided_into_four_parts(x0,y0,C4x,C4y,C5x,C5y, Px, Py, Qx, Qy, Rx, Ry, Sx, Sy ); #T023
#print '4b:',Triangle_divided_into_four_quadrants(x0,y0,C4x,C4y,C5x,C5y); #T023
#print '5', Triangle_divided_into_four_parts(x0,y0,C5x,C5y,C6x,C6y, Px, Py, Qx, Qy, Rx, Ry, Sx, Sy ); #T023
#print '5b',Triangle_divided_into_four_quadrants(x0,y0,C5x,C5y,C6x,C6y); #T023
#print '6:',Triangle_divided_into_four_parts(x0,y0,C6x,C6y,C1x,C1y, Px, Py, Qx, Qy, Rx, Ry, Sx, Sy ); #T023
#print '6b',Triangle_divided_into_four_quadrants(x0,y0,C6x,C6y,C1x,C1y); #T023
#Summing up
Area_hex=T12_Area+T23_Area+T34_Area+T45_Area+T56_Area+T61_Area;
Area_Q1=T12_Q1+T23_Q1+T34_Q1+T45_Q1+T56_Q1+T61_Q1;
Area_Q2=T12_Q2+T23_Q2+T34_Q2+T45_Q2+T56_Q2+T61_Q2;
Area_Q3=T12_Q3+T23_Q3+T34_Q3+T45_Q3+T56_Q3+T61_Q3;
Area_Q4=T12_Q4+T23_Q4+T34_Q4+T45_Q4+T56_Q4+T61_Q4;
#Area_Q4=Area_hex-(Area_Q1+Area_Q2+Area_Q3)
#if (abs(x0-(-0.186672393129))<0.00001) and (abs(y0-(-0.436171))<0.00001):
# print 'Triangle 1,xo,y0',x0,y0
# print 'Triangle 1H,S',H,S
# print 'Triangle 1,Corners',C1x,C1y,C2x,C2y
# print 'Triangle 1, Area full', T12_Area
# print 'Triangle 1, Area T1', T12_Q1
# print 'Triangle 1, Area T2', T12_Q2
# print 'Triangle 1, Area T3', T12_Q3
# print 'Triangle 1, Area T4', T12_Q4
# #print 'Triangle 2',x0,y0,C2x,C2y,C3x,C3y,T23_Area,T23_Q1,T23_Q2,T23_Q3,T23_Q4
# #print 'Triangle 3',x0,y0,C3x,C3y,C4x,C4y,T34_Area,T34_Q1,T34_Q2,T34_Q3,T34_Q4
# #print 'Triangle 4',x0,y0,C4x,C4y,C5x,C5y,T45_Area,T45_Q1,T45_Q2,T45_Q3,T45_Q4
# #print 'Triangle 5',x0,y0,C5x,C5y,C6x,C6y,T56_Area,T56_Q1,T56_Q2,T56_Q3,T56_Q4
# #print 'Triangle 6',x0,y0,C6x,C6y,C1x,C1y,T61_Area,T61_Q1,T61_Q2,T61_Q3,T61_Q4
Area_Q1=max(Area_Q1,0.);
Area_Q2=max(Area_Q2,0.);
Area_Q3=max(Area_Q3,0.);
Area_Q4=max(Area_Q4,0.);
Error=Area_hex-(Area_Q1+Area_Q2+Area_Q3+Area_Q4)
if (abs(Error)>0.01):
print 'diamonds, hex error, H,x0,y0, Error', H, x0 , y0, Error
print 'diamonds, hex error, Areas',Area_hex, (Area_Q1+Area_Q2 + Area_Q3+Area_Q4), Area_Q1, Area_Q2 , Area_Q3, Area_Q4
print 'Hexagon error is large!!'
#Adjust Areas so that the error is zero by subtracting the error from the largest sector.
if (((Area_Q1>=Area_Q2) and (Area_Q1>=Area_Q3)) and (Area_Q1>=Area_Q4)):
#print 'fix1',Error
Area_Q1=Area_Q1+Error
elif (((Area_Q2>=Area_Q1) and (Area_Q2>=Area_Q3)) and (Area_Q2>=Area_Q4)):
#print 'fix2', Error
Area_Q2=Area_Q2+Error
elif (((Area_Q3>=Area_Q1) and (Area_Q3>=Area_Q2)) and (Area_Q3>=Area_Q4)):
#print 'fix3',Error
Area_Q3=Area_Q3+Error
elif (((Area_Q4>=Area_Q1) and (Area_Q4>=Area_Q2)) and (Area_Q4>=Area_Q3)):
#print 'fix4',Error
Area_Q4=Area_Q4+Error
else:
print 'There is some thing wrong with this hexagon. Something very wrong'
#Error=Area_hex-(Area_Q1+Area_Q2+Area_Q3+Area_Q4)
Error=Area_hex-(Area_Q1+Area_Q2+Area_Q3+Area_Q4)
if ((abs(Error)>0.01)):
print 'The hexagon error is still too large!!!', Error
return [Area_hex ,Area_Q1, Area_Q2, Area_Q3, Area_Q4]
def Hexagon_into_quadrants_using_triangles(x0,y0,H,theta):
#Length of side of Hexagon
S=(2/sqrt(3))*H;
#Finding positions of corners
C1x=S ; C1y=0. #Corner 1 (right)
C2x=H/sqrt(3.) ; C2y=H; #Corner 2 (top right)
C3x=-H/sqrt(3.) ; C3y=H; #Corner 3 (top left)
C4x=-S ; C4y=0.; #Corner 4 (left)
C5x=-H/sqrt(3.) ; C5y=-H; #Corner 5 (bottom right)
C6x=H/sqrt(3.) ; C6y=-H; #Corner 6 (bottom right)
#Finding positions of corners
[C1x,C1y]=rotate_and_translate(C1x,C1y,theta,x0,y0)
[C2x,C2y]=rotate_and_translate(C2x,C2y,theta,x0,y0)
[C3x,C3y]=rotate_and_translate(C3x,C3y,theta,x0,y0)
[C4x,C4y]=rotate_and_translate(C4x,C4y,theta,x0,y0)
[C5x,C5y]=rotate_and_translate(C5x,C5y,theta,x0,y0)
[C6x,C6y]=rotate_and_translate(C6x,C6y,theta,x0,y0)
#Area of Hexagon is the sum of the triangles
[T12_Area,T12_Q1,T12_Q2,T12_Q3,T12_Q4]=Triangle_divided_into_four_quadrants(x0,y0,C1x,C1y,C2x,C2y); #T012
[T23_Area,T23_Q1,T23_Q2,T23_Q3,T23_Q4]=Triangle_divided_into_four_quadrants(x0,y0,C2x,C2y,C3x,C3y); #T023
[T34_Area,T34_Q1,T34_Q2,T34_Q3,T34_Q4]=Triangle_divided_into_four_quadrants(x0,y0,C3x,C3y,C4x,C4y); #T034
[T45_Area,T45_Q1,T45_Q2,T45_Q3,T45_Q4]=Triangle_divided_into_four_quadrants(x0,y0,C4x,C4y,C5x,C5y); #T023
[T56_Area,T56_Q1,T56_Q2,T56_Q3,T56_Q4]=Triangle_divided_into_four_quadrants(x0,y0,C5x,C5y,C6x,C6y); #T023
[T61_Area,T61_Q1,T61_Q2,T61_Q3,T61_Q4]=Triangle_divided_into_four_quadrants(x0,y0,C6x,C6y,C1x,C1y); #T023
#Summing up
Area_hex=T12_Area+T23_Area+T34_Area+T45_Area+T56_Area+T61_Area;
Area_Q1=T12_Q1+T23_Q1+T34_Q1+T45_Q1+T56_Q1+T61_Q1;
Area_Q2=T12_Q2+T23_Q2+T34_Q2+T45_Q2+T56_Q2+T61_Q2;
Area_Q3=T12_Q3+T23_Q3+T34_Q3+T45_Q3+T56_Q3+T61_Q3;
Area_Q4=T12_Q4+T23_Q4+T34_Q4+T45_Q4+T56_Q4+T61_Q4;
#Area_Q4=Area_hex-(Area_Q1+Area_Q2+Area_Q3)
#if (abs(x0-(-0.186672393129))<0.00001) and (abs(y0-(-0.436171))<0.00001):
# print 'Triangle 1,xo,y0',x0,y0
# print 'Triangle 1H,S',H,S
# print 'Triangle 1,Corners',C1x,C1y,C2x,C2y
# print 'Triangle 1, Area full', T12_Area
# print 'Triangle 1, Area T1', T12_Q1
# print 'Triangle 1, Area T2', T12_Q2
# print 'Triangle 1, Area T3', T12_Q3
# print 'Triangle 1, Area T4', T12_Q4
# #print 'Triangle 2',x0,y0,C2x,C2y,C3x,C3y,T23_Area,T23_Q1,T23_Q2,T23_Q3,T23_Q4
# #print 'Triangle 3',x0,y0,C3x,C3y,C4x,C4y,T34_Area,T34_Q1,T34_Q2,T34_Q3,T34_Q4
# #print 'Triangle 4',x0,y0,C4x,C4y,C5x,C5y,T45_Area,T45_Q1,T45_Q2,T45_Q3,T45_Q4
# #print 'Triangle 5',x0,y0,C5x,C5y,C6x,C6y,T56_Area,T56_Q1,T56_Q2,T56_Q3,T56_Q4
# #print 'Triangle 6',x0,y0,C6x,C6y,C1x,C1y,T61_Area,T61_Q1,T61_Q2,T61_Q3,T61_Q4
Area_Q1=max(Area_Q1,0.);
Area_Q2=max(Area_Q2,0.);
Area_Q3=max(Area_Q3,0.);
Area_Q4=max(Area_Q4,0.);
Error=Area_hex-(Area_Q1+Area_Q2+Area_Q3+Area_Q4)
if (abs(Error)>0.01):
print 'diamonds, hex error, H,x0,y0, Error', H, x0 , y0, Error
print 'diamonds, hex error, Areas',Area_hex, (Area_Q1+Area_Q2 + Area_Q3+Area_Q4), Area_Q1, Area_Q2 , Area_Q3, Area_Q4
print 'Hexagon error is large!!'
#Adjust Areas so that the error is zero by subtracting the error from the largest sector.
if (((Area_Q1>=Area_Q2) and (Area_Q1>=Area_Q3)) and (Area_Q1>=Area_Q4)):
#print 'fix1',Error
Area_Q1=Area_Q1+Error
elif (((Area_Q2>=Area_Q1) and (Area_Q2>=Area_Q3)) and (Area_Q2>=Area_Q4)):
#print 'fix2', Error
Area_Q2=Area_Q2+Error
elif (((Area_Q3>=Area_Q1) and (Area_Q3>=Area_Q2)) and (Area_Q3>=Area_Q4)):
#print 'fix3',Error
Area_Q3=Area_Q3+Error
elif (((Area_Q4>=Area_Q1) and (Area_Q4>=Area_Q2)) and (Area_Q4>=Area_Q3)):
#print 'fix4',Error
Area_Q4=Area_Q4+Error
else:
print 'There is some thing wrong with this hexagon. Something very wrong'
#Error=Area_hex-(Area_Q1+Area_Q2+Area_Q3+Area_Q4)
Error=Area_hex-(Area_Q1+Area_Q2+Area_Q3+Area_Q4)
if ((abs(Error)>0.01)):
print 'The hexagon error is still too large!!!', Error
return [Area_hex ,Area_Q1, Area_Q2, Area_Q3, Area_Q4]
def find_intersect_using_metric(Ax,Ay, Bx, By, Cx, Cy):
#Line AB
[AB_px, AB_py]=intercept_of_a_line(Ax,Ay,Bx,By,'x'); #x_intercept
[AB_qx, AB_qy]=intercept_of_a_line(Ax,Ay,Bx,By,'y'); #y_intercept
M_AB=point_in_interval_metric(Ax,Ay,Bx,By,AB_px,AB_py, AB_qx, AB_qy)
#Line AC
[AC_px, AC_py]=intercept_of_a_line(Ax,Ay,Cx,Cy,'x'); #x_intercept
[AC_qx, AC_qy]=intercept_of_a_line(Ax,Ay,Cx,Cy,'y'); #y_intercept
M_AC=point_in_interval_metric(Ax,Ay,Cx,Cy,AC_px, AC_py,AC_qx, AC_qy)
#Line AB
[BC_px, BC_py]=intercept_of_a_line(Bx,By,Cx,Cy,'x'); #x_intercept
[BC_qx, BC_qy]=intercept_of_a_line(Bx,By,Cx,Cy,'y'); #y_intercept
M_BC=point_in_interval_metric(Bx,By,Cx,Cy,BC_px,BC_py,BC_qx, BC_qy)
if M_AB<=min(M_BC, M_AC):
return AB_px, AB_py, AB_qx, AB_qy
elif M_AC<=min(M_BC, M_AB):
return AC_px, AC_py, AC_qx, AC_qy
elif M_BC<=min(M_AC, M_AB):
return BC_px, BC_py, BC_qx, BC_qy
else:
print 'You should not get here'
halt
def find_intersect_using_metric_and_lines(Ax,Ay, Bx, By, Cx, Cy,Px, Py, Qx, Qy, Rx, Ry, Sx, Sy):
#Line AB
[AB_px, AB_py]=intercept_of_two_lines(Ax,Ay,Bx,By,Px, Py, Qx, Qy); #PQ_intercept
[AB_qx, AB_qy]=intercept_of_two_lines(Ax,Ay,Bx,By,Rx, Ry, Sx, Sy); #RS_intercept
M_AB=point_in_interval_metric(Ax,Ay,Bx,By,AB_px,AB_py, AB_qx, AB_qy)
#Line AC
[AC_px, AC_py]=intercept_of_two_lines(Ax,Ay,Cx,Cy,Px, Py, Qx, Qy); #PQ_intercept
[AC_qx, AC_qy]=intercept_of_two_lines(Ax,Ay,Cx,Cy, Rx, Ry, Sx, Sy); #RS_intercept
M_AC=point_in_interval_metric(Ax,Ay,Cx,Cy,AC_px, AC_py,AC_qx, AC_qy)
#Line AB
[BC_px, BC_py]=intercept_of_two_lines(Bx,By,Cx,Cy,Px, Py, Qx, Qy); #PQ_intercept
[BC_qx, BC_qy]=intercept_of_two_lines(Bx,By,Cx,Cy, Rx, Ry, Sx, Sy); #RS_intercept
M_BC=point_in_interval_metric(Bx,By,Cx,Cy,BC_px,BC_py,BC_qx, BC_qy)
if M_AB<=min(M_BC, M_AC):
return AB_px, AB_py, AB_qx, AB_qy
elif M_AC<=min(M_BC, M_AB):
return AC_px, AC_py, AC_qx, AC_qy
elif M_BC<=min(M_AC, M_AB):
return BC_px, BC_py, BC_qx, BC_qy
else:
print 'You should not get here'
halt
def point_in_interval_metric(Ax,Ay,Bx,By,px,py,qx,qy):
metric=0.0
#print 'Ax,Ay,Bx,By',Ax,Ay,Bx,By,px,py,qx,qy
#print 'px,py,qx,qy',px,py,qx,qy
#Finds a metric for how close a point is to being inside an interval. If it is inside, then metric is equal to zero.
Mx1= max(px - max(Ax,Bx),0.) #Zero is px <= max(Ax,Bx)
Mx2= max( min(Ax,Bx)-px ,0.) #Zero is px <= max(Ax,Bx)
My1= max(py - max(Ay,By),0.) #Zero is px <= max(Ax,Bx)
My2= max( min(Ay,By)-py ,0.) #Zero is px <= max(Ax,Bx)
p_metric=abs(Mx1)+abs(Mx2)+abs(My1)+abs(My2)
#print 'Mx1, Mx2, My1, My2', Mx1, Mx2, My1, My2
#Finds a metric for how close a point is to being inside an interval. If it is inside, then metric is equal to zero.
Mx1= max(qx - max(Ax,Bx),0.) #Zero is qx <= max(Ax,Bx)
Mx2= max( min(Ax,Bx)-qx ,0.) #Zero is qx <= max(Ax,Bx)
My1= max(qy - max(Ay,By),0.) #Zero is qx <= max(Ax,Bx)
My2= max( min(Ay,By)-qy ,0.) #Zero is qx <= max(Ax,Bx)
q_metric=abs(Mx1)+abs(Mx2)+abs(My1)+abs(My2)
metric=p_metric+q_metric
return metric
def Triangle_divided_into_four_quadrants(Ax,Ay,Bx,By,Cx,Cy):
Area_key_quadrant=0.0 #Initializing
Area_triangle=Area_of_triangle(Ax,Ay,Bx,By,Cx,Cy);
#if Area_triangle==0.0:
# Area_triangle=0.0 ; Area_Q1=0.0 ; Area_Q2=0.0; Area_Q3=0.0; Area_Q4= 0.0
# return [Area_triangle, Area_Q1, Area_Q2 ,Area_Q3 ,Area_Q4]
#Calculating area across axes
[Area_Upper ,Area_Lower]=divding_triangle_across_axes(Ax,Ay,Bx,By,Cx,Cy,'x');
[Area_Right ,Area_Left]=divding_triangle_across_axes(Ax,Ay,Bx,By,Cx,Cy,'y');
#Decide if the origin is in the triangle
if point_in_triangle(Ax,Ay,Bx,By,Cx,Cy,0.,0.): #Then you have to divide area 4 ways.
#Find a line in the triangle that cuts both axes in/on the trianlge
[px, py]=intercept_of_a_line(Ax,Ay,Bx,By,'x'); #x_intercept
[qx, qy]=intercept_of_a_line(Ax,Ay,Bx,By,'y'); #y_intercept
if (point_in_interval(Ax,Ay,Bx,By,px,py) & point_in_interval(Ax,Ay,Bx,By,qx,qy))==False:
[px, py]=intercept_of_a_line(Ax,Ay,Cx,Cy,'x'); #x_intercept
[qx, qy]=intercept_of_a_line(Ax,Ay,Cx,Cy,'y'); #y_intercept
if (point_in_interval(Ax,Ay,Cx,Cy,px,py) & point_in_interval(Ax,Ay,Cx,Cy,qx,qy))==False:
[px, py]=intercept_of_a_line(Bx,By,Cx,Cy,'x'); #x_intercept
[qx, qy]=intercept_of_a_line(Bx,By,Cx,Cy,'y'); #y_intercept
if (point_in_interval(Bx,By,Cx,Cy,px,py) & point_in_interval(Bx,By,Cx,Cy,qx,qy))==False:
#point_in_interval_metric
[px, py, qx, qy]= find_intersect_using_metric(Ax,Ay, Bx, By, Cx, Cy)
#print 'Houston, we have a problem'
#plot_axes_and_triangel(Ax,Ay, Bx, By, Cx, Cy)
#halt
#Assigning quadrants. Key_quadrant is the quadrant with the baby triangle in it.
Area_key_quadrant=Area_of_triangle(px,py,qx,qy,0.,0.);
if Area_key_quadrant>0.0:
if px>=0. and qy>=0.: #First quadrant
Key_quadrant=1;
elif px<0. and qy>=0.: #Second quadrant
Key_quadrant=2;
elif px<0. and qy<0.:
Key_quadrant=3; #Third quadrant
else:
Key_quadrant=4; #Forth quadrant
#if (point_in_triangle(Ax,Ay,Bx,By,Cx,Cy,0.,0.) is False) or (Area_key_quadrant==0):
if Area_key_quadrant==0:
#else: #Then at least one quadrant is empty, and this can be used to find the areas in the other quadrant. Assigning quadrants. Key_quadrant is the empty quadrant.
#print 'Mother...'
#print 'Ax, Ay',Ax,Ay
#print 'Bx, By',Bx,By
#print 'Cx, Cy',Cx,Cy
Area_key_quadrant=0;
if (((Ax>0. and Ay>0.) or (Bx>0. and By>0.) or (Cx>0. and Cy>0.))==False) and (Area_Upper+Area_Right<=Area_triangle):
#if (((Ax>=0. and Ay>=0.) or (Bx>=0. and By>=0.) or (Cx>=0. and Cy>=0.))==False) and (Area_Upper+Area_Right<=Area_triangle):
#No points land in this quadrant and triangle does not cross the quadrant
Key_quadrant=1;
elif (((Ax<0. and Ay>0) or (Bx<0. and By>0.) or (Cx<0. and Cy>0.))==False) and (Area_Upper+Area_Left<=Area_triangle):
#elif (((Ax<0. and Ay>=0) or (Bx<0. and By>=0.) or (Cx<0. and Cy>=0.))==False) and (Area_Upper+Area_Left<=Area_triangle):
Key_quadrant=2;
elif (((Ax<0. and Ay<0.) or (Bx<0. and By<0.) or (Cx<0. and Cy<0.))==False) & (Area_Lower+Area_Left<=Area_triangle):
#elif (((Ax<0. and Ay<0.) or (Bx<0. and By<0.) or (Cx<0. and Cy<0.))==False) & (Area_Lower+Area_Left<=Area_triangle):
Key_quadrant=3;
else:
Key_quadrant=4;
#Assign values to quadrants
if Key_quadrant==1:
Area_Q1=Area_key_quadrant;
Area_Q2=Area_Upper-Area_Q1;
Area_Q4=Area_Right-Area_Q1;
#Area_Q3=Area_Left-Area_Q2;
Area_Q3=Area_triangle-Area_Q1-Area_Q2-Area_Q4;
elif Key_quadrant==2:
Area_Q2=Area_key_quadrant;
Area_Q1=Area_Upper-Area_Q2;
Area_Q4=Area_Right-Area_Q1;
#Area_Q3=Area_Left-Area_Q2;
Area_Q3=Area_triangle-Area_Q1-Area_Q2-Area_Q4;
elif Key_quadrant==3:
Area_Q3=Area_key_quadrant;
Area_Q2=Area_Left-Area_Q3;
Area_Q1=Area_Upper-Area_Q2;
#Area_Q4=Area_Right-Area_Q1;
Area_Q4=Area_triangle-Area_Q1-Area_Q2-Area_Q3;
elif Key_quadrant==4:
Area_Q4=Area_key_quadrant;
Area_Q1=Area_Right-Area_Q4;
Area_Q2=Area_Upper-Area_Q1;
#Area_Q3=Area_Left-Area_Q2;
Area_Q3=Area_triangle-Area_Q1-Area_Q2-Area_Q4;
else:
print 'Help, I need somebody, help!'
halt
Area_Q1=max(Area_Q1,0.);
Area_Q2=max(Area_Q2,0.);
Area_Q3=max(Area_Q3,0.);
Area_Q4=max(Area_Q4,0.);
Error=abs(Area_Q1+Area_Q2+Area_Q3+Area_Q4-Area_triangle)
#Marker 1
if Error>1e-15:
#if True:
print 'The triangles are not accurate enough. This is a problem!'
print 'Triangle corners: ' ,Ax,Ay,Bx,By,Cx,Cy
print 'Error',Error
print 'Key_quadrant', Key_quadrant
print 'Area Key_quadrant', Area_key_quadrant
print 'Upper, Lower',Area_Upper ,Area_Lower
print 'Left, Right ', Area_Right ,Area_Left
print 'Point in triangle', point_in_triangle(Ax,Ay,Bx,By,Cx,Cy,0.,0.)
#plt.plot(np.array([Ax,Bx,Cx,Ax]),np.array([Ay, By, Cy,Ay]))
#plt.plot(np.array([-0.5,0.5]),np.array([0.,0.]),'k')
#plt.plot(np.array([0.,0.]),np.array([-0.5,0.5]),'k')
#plt.plot(0,0,'*')
#plt.show()
#halt
#return
return [Area_triangle, Area_Q1, Area_Q2 ,Area_Q3 ,Area_Q4]
def find_quadrant_using_lines(x0 , y0 , Px, Py, Qx, Qy, Rx, Ry, Sx, Sy): #Finding "quadrant" of point using lines PQ, RS as axes. (This assumes PQ is like the x axis and RS like the y axis)
#above_PQ=Point_above_line(x0,y0, Px, Py, Qx, Qy)
#above_RS=Point_above_line(x0,y0, Rx, Ry, Sx, Sy)
above_PQ=Above_on_below_line(x0,y0, Px, Py, Qx, Qy)
above_RS=Above_on_below_line(x0,y0, Rx, Ry, Sx, Sy)
if (above_PQ==1) and (above_RS==1):
quadrant=1
elif (above_PQ==1) and (above_RS==-1):
quadrant=2
elif (above_PQ==-1) and (above_RS==-1):
quadrant=3
elif (above_PQ==-1) and (above_RS==1):
quadrant=4
else:
#print 'Point not in a quadrant!'
quadrant=0
#halt
return quadrant
def Triangle_divided_into_four_parts(Ax,Ay,Bx,By,Cx,Cy, Px, Py, Qx, Qy, Rx, Ry, Sx, Sy ):#Divides the triangle into 4 parts, across two intersecting lines. PQ, RS intersect at origin (for now)
Area_key_quadrant=0.0
Area_triangle=Area_of_triangle(Ax,Ay,Bx,By,Cx,Cy);
#if Area_triangle==0.0:
# Area_triangle=0.0 ; Area_Q1=0.0 ; Area_Q2=0.0; Area_Q3=0.0; Area_Q4= 0.0
# return [Area_triangle, Area_Q1, Area_Q2 ,Area_Q3 ,Area_Q4]
#Calculating area across axes
[Area_Upper ,Area_Lower]=divding_triangle_across_line(Ax,Ay,Bx,By,Cx,Cy, Px, Py, Qx, Qy);
[Area_Right ,Area_Left] =divding_triangle_across_line(Ax,Ay,Bx,By,Cx,Cy, Rx, Ry, Sx, Sy);
#Decide if the origin is in the triangle
if point_in_triangle(Ax,Ay,Bx,By,Cx,Cy,0.,0.): #Then you have to divide area 4 ways.
#Find a line in the triangle that cuts both axes in/on the trianlge
[px, py]=intercept_of_two_lines(Ax,Ay,Bx,By,Px, Py, Qx, Qy); #PQ_intercept
[qx, qy]=intercept_of_two_lines(Ax,Ay,Bx,By,Rx, Ry, Sx, Sy); #RS_intercept
if (point_in_interval(Ax,Ay,Bx,By,px,py) & point_in_interval(Ax,Ay,Bx,By,qx,qy))==False:
[px, py]=intercept_of_two_lines(Ax,Ay,Cx,Cy,Px, Py, Qx, Qy); #PQ_intercept
[qx, qy]=intercept_of_two_lines(Ax,Ay,Cx,Cy,Rx, Ry, Sx, Sy); #RS_intercept
if (point_in_interval(Ax,Ay,Cx,Cy,px,py) & point_in_interval(Ax,Ay,Cx,Cy,qx,qy))==False:
[px, py]=intercept_of_two_lines(Bx,By,Cx,Cy,Px, Py, Qx, Qy); #PQ_intercept
[qx, qy]=intercept_of_two_lines(Bx,By,Cx,Cy,Rx, Ry, Sx, Sy); #RS_intercept
if (point_in_interval(Bx,By,Cx,Cy,px,py) & point_in_interval(Bx,By,Cx,Cy,qx,qy))==False:
#print 'Houston, we have a problem'
[px, py, qx, qy]= find_intersect_using_metric_and_lines(Ax,Ay, Bx, By, Cx, Cy,Px, Py, Qx, Qy, Rx, Ry, Sx, Sy)
#plot_axes_and_triangel(Ax,Ay, Bx, By, Cx, Cy)
#halt
#Assigning quadrants. Key_quadrant is the quadrant with the baby triangle in it.
Area_key_quadrant=Area_of_triangle(px,py,qx,qy,0.,0.);
if px>=0. and qy>=0.: #First quadrant (I think the zeros here are because the PQ, RS intersect at zero)
Key_quadrant=1;
elif px<0. and qy>=0.: #Second quadrant
Key_quadrant=2;
elif px<0. and qy<0.:
Key_quadrant=3; #Third quadrant
else:
Key_quadrant=4; #Forth quadrant
if Area_key_quadrant==0:
#else: #Then at least one quadrant is empty, and this can be used to find the areas in the other quadrant. Assigning quadrants. Key_quadrant is the empty quadrant.
#print 'Mother...'
#print 'Ax, Ay',Ax,Ay
#print 'Bx, By',Bx,By
#print 'Cx, Cy',Cx,Cy
Area_key_quadrant=0;
#Finding which quadrant the triangle points fall in.
A_quad = find_quadrant_using_lines(Ax,Ay,Px, Py, Qx, Qy, Rx, Ry, Sx, Sy)
B_quad = find_quadrant_using_lines(Bx,By,Px, Py, Qx, Qy, Rx, Ry, Sx, Sy)
C_quad = find_quadrant_using_lines(Cx,Cy,Px, Py, Qx, Qy, Rx, Ry, Sx, Sy)
#print 'A_quad, B_quad, C_quad',A_quad, B_quad, C_quad
if (((A_quad==1) or (B_quad==1) or (C_quad==1))==False) and (Area_Upper+Area_Right<=Area_triangle):
#No points land in this quadrant and triangle does not cross the quadrant
Key_quadrant=1;
elif (((A_quad==2) or (B_quad==2) or (C_quad==2))==False) and (Area_Upper+Area_Left<=Area_triangle):
Key_quadrant=2;
elif (((A_quad==3) or (B_quad==3) or (C_quad==3))==False) & (Area_Lower+Area_Left<=Area_triangle):
Key_quadrant=3;
else:
Key_quadrant=4;
#print Key_quadrant
#Assign values to quadrants
if Key_quadrant==1:
Area_Q1=Area_key_quadrant;
Area_Q2=Area_Upper-Area_Q1;
Area_Q4=Area_Right-Area_Q1;
#Area_Q3=Area_Left-Area_Q2;
Area_Q3=Area_triangle-Area_Q1-Area_Q2-Area_Q4;
elif Key_quadrant==2:
Area_Q2=Area_key_quadrant;
Area_Q1=Area_Upper-Area_Q2;
Area_Q4=Area_Right-Area_Q1;
#Area_Q3=Area_Left-Area_Q2;
Area_Q3=Area_triangle-Area_Q1-Area_Q2-Area_Q4;
elif Key_quadrant==3:
Area_Q3=Area_key_quadrant;
Area_Q2=Area_Left-Area_Q3;
Area_Q1=Area_Upper-Area_Q2;
#Area_Q4=Area_Right-Area_Q1;
Area_Q4=Area_triangle-Area_Q1-Area_Q2-Area_Q3;
elif Key_quadrant==4:
Area_Q4=Area_key_quadrant;
Area_Q1=Area_Right-Area_Q4;
Area_Q2=Area_Upper-Area_Q1;
#Area_Q3=Area_Left-Area_Q2;
Area_Q3=Area_triangle-Area_Q1-Area_Q2-Area_Q4;
else:
print 'Help, I need somebody, help!'
halt
Area_Q1=max(Area_Q1,0.);
Area_Q2=max(Area_Q2,0.);
Area_Q3=max(Area_Q3,0.);
Area_Q4=max(Area_Q4,0.);
Error=abs(Area_Q1+Area_Q2+Area_Q3+Area_Q4-Area_triangle)
if Error>1e-15:
#if True:
print 'The triangles are not accurate enough. This is a problem!'
print 'Triangle corners: ' ,Ax,Ay,Bx,By,Cx,Cy
print 'Error',Error
print 'Key_quadrant', Key_quadrant
print 'Area Key_quadrant', Area_key_quadrant
print 'Upper, Lower',Area_Upper ,Area_Lower
print 'Left, Right ', Area_Right ,Area_Left
print 'Point in triangle', point_in_triangle(Ax,Ay,Bx,By,Cx,Cy,0.,0.)
#return
return [Area_triangle, Area_Q1, Area_Q2 ,Area_Q3 ,Area_Q4]
def divding_triangle_across_axes(Ax,Ay,Bx,By,Cx,Cy,axes1):
if axes1=='x': #Use the y-coordinates for if statements to see which side of the line you are on
A0=Ay; B0=By; C0=Cy;
if axes1=='y': #Use the y-coordinates for if statements to see which side of the line you are on
A0=Ax; B0=Bx; C0=Cx;
#print 'A0,B0,C0', A0,B0,C0
A_triangle=Area_of_triangle(Ax,Ay,Bx,By,Cx,Cy);
if B0*C0>0.: #then B and C are on the same side (and non-zero)
#print 'HHHH1'
if A0*B0>=0.: #then all three on the the same side (if it equals zero, then A0=0 and the otehrs are not)
if (A0>0.) or (A0==0. and B0>0.):
Area_positive= A_triangle;
Area_negative= 0.;
else:
Area_positive= 0.;
Area_negative= A_triangle;
else: #A is on the opposite side to B and C
[Area_positive, Area_negative]=Area_of_triangle_across_axes(Ax,Ay,Bx,By,Cx,Cy,axes1);
elif B0*C0<0.: #then B and C are on the opposite sides
#print 'HHHH2'
if A0*B0>=0.: #then C is all alone
[Area_positive, Area_negative]=Area_of_triangle_across_axes(Cx,Cy,Bx,By,Ax,Ay,axes1);
else: #then B is all alone
[Area_positive, Area_negative]=Area_of_triangle_across_axes(Bx,By,Cx,Cy,Ax,Ay,axes1);
else: #This is the case when either B or C is equal to zero (or both), A0 could be zero too.
#print 'HHHH3'
if (A0==0. and B0==0. and C0==0.):
Area_positive= 0.;
Area_negative= 0.;
elif (A0*B0<0.) or (A0*C0<0.): #A, B are on opposite sides, and C is zero. OR A, C are on opposite sides, and B is zero.
[Area_positive, Area_negative]=Area_of_triangle_across_axes(Ax,Ay,Bx,By,Cx,Cy,axes1);
#print 'HHHH6'
elif (A0*B0>0.) or (A0*C0>0.) or (abs(A0)>0. and (B0==0.) and (C0==0.)):
#print 'HHHH7'
if (A0>0.):
Area_positive= A_triangle;
Area_negative= 0.;
else:
Area_positive= 0.;
Area_negative= A_triangle;
elif A0==0.: #(Also, one of B,C is zero too)
#print 'HHHH8'
if B0>0. or C0>0.:
Area_positive= A_triangle;
Area_negative= 0.;
elif B0<0. or C0<0.:
Area_positive= 0.;
Area_negative= A_triangle;
else:
print 'You should not get here1'
halt
else:
print 'You should not get here2'
print Ax,Ay,Bx,By,Cx,Cy
halt
return [Area_positive, Area_negative]
def divding_triangle_across_line2(Ax,Ay,Bx,By,Cx,Cy, Px, Py, Qx, Qy ):
A0= Above_on_below_line(Ax,Ay, Px, Py, Qx, Qy)
B0= Above_on_below_line(Bx,By, Px, Py, Qx, Qy)
C0= Above_on_below_line(Cx,Cy, Px, Py, Qx, Qy)
#print 'A0,B0,C0', A0,B0,C0
#print Px, Py, Qx, Qy
A_triangle=Area_of_triangle(Ax,Ay,Bx,By,Cx,Cy);
if B0*C0>0.: #then B and C are on the same side (and non-zero)
#print 'GGG1'
if A0*B0>=0.: #then all three on the the same side (if it equals zero, then A0=0 and the otehrs are not)
if (A0>0.) or (A0==0. and B0>0.):
Area_positive= A_triangle;
Area_negative= 0.;
else:
Area_positive= 0.;
Area_negative= A_triangle;
else: #A is on the opposite side to B and C
[Area_positive, Area_negative]=Area_of_triangle_across_line(Ax,Ay,Bx,By,Cx,Cy, Px, Py, Qx, Qy );
elif B0*C0<0.: #then B and C are on the opposite sides
#print 'GGG2'
if A0*B0>=0.: #then C is all alone
[Area_positive, Area_negative]=Area_of_triangle_across_line(Cx,Cy,Bx,By,Ax,Ay, Px, Py, Qx, Qy );
else: #then B is all alone
[Area_positive, Area_negative]=Area_of_triangle_across_line(Bx,By,Cx,Cy,Ax,Ay, Px, Py, Qx, Qy );
else: #This is the case when either B or C is equal to zero (or both), A0 could be zero too.
#print 'GGG3'
if (A0==0. and B0==0. and C0==0.):
Area_positive= 0.;
Area_negative= 0.;
elif (A0*B0<0.) or (A0*C0<0.): #A, B are on opposite sides, and C is zero. OR A, C are on opposite sides, and B is zero.
[Area_positive, Area_negative]=Area_of_triangle_across_line(Ax,Ay,Bx,By,Cx,Cy, Px, Py, Qx, Qy);
#print 'GGG5', Area_positive, Area_negative
elif (A0*B0>0.) or (A0*C0>0.) or (abs(A0)>0. and (B0==0.) and (C0==0.)):
if (A0>0.):
Area_positive= A_triangle;
Area_negative= 0.;
else:
Area_positive= 0.;
Area_negative= A_triangle;
elif A0==0.: #(Also, one of B,C is zero too)
if B0>0. or C0>0.:
Area_positive= A_triangle;
Area_negative= 0.;
elif B0<0. or C0<0.:
Area_positive= 0.;
Area_negative= A_triangle;
else:
print 'You should not get here1'
halt
else:
print 'You should not get here2'
print Ax,Ay,Bx,By,Cx,Cy
halt
return [Area_positive, Area_negative]
def divding_triangle_across_line(Ax,Ay,Bx,By,Cx,Cy, Px, Py, Qx, Qy ): #Triangle is ABC, line is PQ
A0= Above_on_below_line(Ax,Ay, Px, Py, Qx, Qy)
B0= Above_on_below_line(Bx,By, Px, Py, Qx, Qy)
C0= Above_on_below_line(Cx,Cy, Px, Py, Qx, Qy)
#print 'BBB0', A0, B0, C0
A_triangle=Area_of_triangle(Ax,Ay,Bx,By,Cx,Cy);
# B and C are on the same side of the line
#if (Point_above_line(Bx,By, Px, Py, Qx, Qy)==Point_above_line(Cx,Cy, Px, Py, Qx, Qy)) :
#if ((B0==C0) and (B0!=0)) or ((B0==0 or C0==0) and np.max(B0,C0)>0.5) :
if ((B0==C0) and (B0!=0)) or ((B0==0 or C0==0)) : #B and C on the same side or B or C is zero
#print 'BBB1'
[Area_positive, Area_negative]=Area_of_triangle_across_line(Ax,Ay,Bx,By,Cx,Cy, Px, Py, Qx, Qy);
else: #then B and C are on the opposite sides
#if Point_above_line(Ax,Ay, Px, Py, Qx, Qy)==Point_above_line(Bx,By, Px, Py, Qx, Qy): #then C is all alone
if A0*B0>=0.: #then C is all alone
#print 'BBB2'
[Area_positive, Area_negative]=Area_of_triangle_across_line(Cx,Cy,Bx,By,Ax,Ay, Px, Py, Qx, Qy);
else: #then B is all alone
#print 'BBB3'
[Area_positive, Area_negative]=Area_of_triangle_across_line(Bx,By,Cx,Cy,Ax,Ay, Px, Py, Qx, Qy);
return [Area_positive, Area_negative]
def check_if_point_is_on_the_line(Ax,Ay,Bx,By,qx,qy,repeat_calculation=False):
tol=0.00000000000000;
dxc = qx - Ax;
dyc = qy - Ay;
dxl = Bx - Ax;
dyl = By - Ay;
cross = dxc * dyl - dyc * dxl;
if abs(cross)<=tol:
point_is_on_line=True
else:
point_is_on_line=False
if repeat_calculation is False:
if point_is_on_line != check_if_point_is_on_the_line(Bx,By,Ax,Ay,qx,qy, True):
point_is_on_line=True
return point_is_on_line
def intercept_of_a_line(Ax,Ay,Bx,By,axes1):
No_intercept_val=100000000000.; #Huge value used to make sure that the intercept is outside the triange in the parralel case.
#No_intercept_val=np.NaN;
if axes1=='x': #x intercept
if (Ay==By)==False:
x0=Ax -(((Ax-Bx)/(Ay-By))*Ay);
y0=0.;
else:
x0=No_intercept_val;
y0=No_intercept_val;
if axes1=='y': #y intercept
if (Ax==Bx)==False:
x0=0.;
y0=-(((Ay-By)/(Ax-Bx))*Ax)+Ay;
else:
x0=No_intercept_val;
y0=No_intercept_val;
return [x0, y0]
def intercept_of_two_lines(Ax,Ay,Bx,By,Px,Py,Qx,Qy):
No_intercept_val=100000000000.; #Huge value used to make sure that the intercept is outside the triange in the parralel case.
#No_intercept_val=np.NaN;
#One of the lines is actually a point:
if ((Ax==Bx) and (Ay==By)) or ((Px==Qx) and (Py==Qy)):
x0=No_intercept_val; y0=No_intercept_val;
halt6
else:
if (Ax==Bx):
(x0 , y0) =intercept_of_a_line(Px-Ax,Py,Qx-Ax,Qy,'y')
x0=Ax
#Here we check the other three directions to make code agree to machine precision with old version. The code could skip straight to the else.
elif (Px==Qx):
(x0 , y0) =intercept_of_a_line(Ax-Px,Ay,Bx-Px,By,'y')
x0=Px
elif (Ay==By):
(x0 , y0) =intercept_of_a_line(Px,Py-Ay,Qx,Qy-Ay,'x')
y0=Ay
elif (Py==Qy):
(x0 , y0) =intercept_of_a_line(Ax,Ay-Py,Bx,By-Py,'x')
y0=Py
else:
m1=(Ay-By)/(Ax-Bx)
if (Px==Qx):
print 'Stop2'
(x0 , y0) =intercept_of_a_line(Ax-Px,Ay,Bx-Px,By,'y')
x0=Px
else:
m2=(Py-Qy)/(Px-Qx)
if m1==m2:
print 'Stop3'
x0=No_intercept_val;