@@ -702,3 +702,156 @@ List<PointF> points = new List<PointF>();
702702 result.setRightMost(right.getRightMostPoint());
703703 return result;
704704 //}
705+
706+ //merge--------------------------------------------------------
707+ bool leftSideChanged = true;
708+ bool rightSideChanged = true;
709+ int rightMostInLeft = left.getRightMostIndex();
710+ int leftUpperIndex = rightMostInLeft;
711+ int rightUpperIndex = 0;
712+ //get upper common tangent\
713+ while (leftSideChanged || rightSideChanged) {
714+ int newIndex = getLeftUpper(left, right, leftUpperIndex, rightUpperIndex);
715+ if (leftUpperIndex == newIndex) {
716+ leftSideChanged = false;
717+ }
718+ else {
719+ leftUpperIndex = newIndex;
720+ leftSideChanged = true;
721+ }
722+
723+ newIndex = getRightUpper(left, right, leftUpperIndex, rightUpperIndex);
724+ if (rightUpperIndex == newIndex) {
725+ rightSideChanged = false;
726+ }
727+ else {
728+ rightSideChanged = true;
729+ rightUpperIndex = newIndex;
730+ }
731+ }
732+ int leftLowerIndex = rightMostInLeft;
733+ int rightLowerIndex = 0;
734+
735+ leftSideChanged = true;
736+ rightSideChanged = true;
737+
738+ //get lower common tangent
739+ while (leftSideChanged || rightSideChanged) {
740+ int newIndex = getLeftLower(left, right, leftLowerIndex, rightLowerIndex);
741+ if (newIndex == leftLowerIndex) {
742+ leftSideChanged = false;
743+ }
744+ else {
745+ leftLowerIndex = newIndex;
746+ leftSideChanged = true;
747+ }
748+
749+ newIndex = getRightLower(left, right, leftLowerIndex, rightLowerIndex);
750+ if (newIndex == rightLowerIndex) {
751+ rightSideChanged = false;
752+ }
753+ else {
754+ rightSideChanged = true;
755+ rightLowerIndex = newIndex;
756+ }
757+
758+ }
759+
760+ List<PointF> points = join(left, right, leftUpperIndex, rightUpperIndex, leftLowerIndex, rightLowerIndex);
761+ Hull result = new Hull(points);
762+ return result;
763+
764+
765+ //merge--------------------------------------------------------
766+ Boolean foundUpperCommonTangent = false;
767+
768+ int leftMostInRightIndex = 0;
769+ int rightMostInleftIndex = left.getRightMostIndex();
770+
771+ int leftUpperIndex = rightMostInleftIndex;
772+ int rightUpperIndex = leftMostInRightIndex;
773+
774+ while(!foundUpperCommonTangent){
775+ foundUpperCommonTangent = true;
776+ for (int i = 0; i < left.getPoints().Count;i++){
777+ if(leftUpperIndex == i){
778+ continue;
779+ }
780+ if(isAbove(left.getPoints()[leftUpperIndex], right.getPoints()[rightUpperIndex], left.getPoints()[i]) <= 0){
781+ leftUpperIndex = i;
782+ foundUpperCommonTangent = false;
783+ }
784+ }
785+
786+ for (int i = 0; i < right.getPoints().Count;i++){
787+ if(rightUpperIndex == i){
788+ continue;
789+ }
790+ if(isAbove(left.getPoints()[leftUpperIndex], right.getPoints()[rightUpperIndex], right.getPoints()[i]) >= 0){
791+ rightUpperIndex = i;
792+ foundUpperCommonTangent = false;
793+ }
794+ }
795+ }
796+
797+ int leftLowerIndex = rightMostInleftIndex;
798+ int rightLowerIndex = leftMostInRightIndex;
799+
800+ Boolean foundLowerCommonTangent = false;
801+ while(!foundLowerCommonTangent){
802+ //reset flag
803+ foundLowerCommonTangent = true;
804+
805+ for (int i = 0; i < left.getPoints().Count;i++){
806+ //skip current index
807+ if(leftLowerIndex == i){
808+ continue;
809+ }
810+ if(isAbove(left.getPoints()[leftLowerIndex], right.getPoints()[rightLowerIndex], left.getPoints()[i]) >= 0){
811+ leftLowerIndex = i;
812+ foundLowerCommonTangent = false;
813+ }
814+ }
815+
816+ for (int i = 0; i < right.getPoints().Count;i++){
817+ if(rightLowerIndex == i){
818+ continue;
819+ }
820+ if(isAbove(left.getPoints()[leftLowerIndex], right.getPoints()[rightLowerIndex], right.getPoints()[i]) <= 0){
821+ rightLowerIndex = i;
822+ foundLowerCommonTangent = false;
823+ }
824+ }
825+ }
826+
827+ List<PointF> rightPoints = collectPoints(rightUpperIndex, rightLowerIndex, right.getPoints());
828+ List<PointF> leftPoints = collectPoints(leftLowerIndex, leftUpperIndex, left.getPoints());
829+ List<PointF> result = new List<PointF>();
830+ result.AddRange(rightPoints);
831+ result.AddRange(leftPoints);
832+ return new Hull(result);
833+ }
834+
835+ private double isAbove(PointF left, PointF right, PointF test){
836+ double slope = calculateSlope(left, right);
837+ double check = -slope * right.X + right.Y;
838+ return slope * test.X + check - test.Y;
839+ }
840+
841+ private List<PointF> collectPoints(int a, int b, List<PointF> points){
842+ List<PointF> result = new List<PointF>();
843+ if(a <= b){
844+ for (int i = a; i <= b;i++){
845+ result.Add(points[i]);
846+ }
847+ }
848+ else{
849+ for (int i = a; i < points.Count;i++){
850+ result.Add(points[i]);
851+ }
852+ for (int i = 0; i <= b;i++){
853+ result.Add(points[i]);
854+ }
855+ }
856+ return result;
857+ }
0 commit comments