Click here to Skip to main content
15,867,686 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
C++
- (float) getVerticalOffset:(CGPoint)point1 point2:(CGPoint)point2 point3:(CGPoint)point3 point4:(CGPoint)point4 magnetLen:(float)length{
    float result = length;
    if (point1.x > point2.x){
        float t = point1.x; point1.x = point2.x; point2.x = t;
    }
    point1.x -= length;
    point2.x += length;
    if (point3.x > point4.x){
        float t = point3.x; point3.x = point4.x; point4.x = t;
    }
    if ((point1.x < point3.x && point3.x < point2.x)
        || (point1.x < point4.x && point4.x < point2.x)
        || (point3.x < point1.x && point1.x < point4.x)
        || (point3.x < point2.x && point2.x < point4.x)){
        if (fabs(point3.y-point1.y) < length){
            result = point3.y-point1.y;
        }
    }
    return result;
}

- (float) getHorizonOffset:(CGPoint)point1 point2:(CGPoint)point2 point3:(CGPoint)point3 point4:(CGPoint)point4 magnetLen:(float)length{
    float result = length;
    if (point1.y > point2.y){
        float t = point1.y; point1.y = point2.y; point2.y = t;
    }
    point1.y -= length;
    point2.y += length;
    if (point3.y > point4.y){
        float t = point3.y; point3.y = point4.y; point4.y = t;
    }
    if ((point1.y < point3.y && point3.y < point2.y)
        || (point1.y < point4.y && point4.y < point2.y)
        || (point3.y < point1.y && point1.y < point4.y)
        || (point3.y < point2.y && point2.y < point4.y)){
        if (fabs(point3.x-point1.x) < length){
            result = point3.x-point1.x;
        }
    }
    return result;
}

- (CGSize) magnetProccess:(CGRect)rect1 top:(bool)top bottom:(bool)bottom left:(bool)left right:(bool)right{
    float magnetLength = 20;
    CGRect parentRect = self.view.superview.frame;
    NSMutableArray* arrView = [delegate getAllMDIView];
    
    float minHorOffset = magnetLength, minVerOffset = magnetLength;
    NSInteger count = arrView.count;
    
    for (int k=0;k <= count;k ++){
        CGRect rect2;
        if (k < count){
            MovableViewCtr* view = arrView[k];
            if (view != self){
                rect2 = view.view.frame;
            }else{
                continue;
            }
        }else{
            rect2 = parentRect;
        }
        
        float arr1[4][2] = {{0, 0}, {rect1.size.width, 0}, {rect1.size.width, rect1.size.height}, {0, rect1.size.height}};
        float arr2[4][2] = {{0, 0}, {rect2.size.width, 0}, {rect2.size.width, rect2.size.height}, {0, rect2.size.height}};
        
        CGPoint basePoint1 = rect1.origin;
        CGPoint basePoint2 = rect2.origin;
        
        for (int i=0;i < 4;i ++){
            if ((!top && i == 0) || (!right && i == 1) || (!bottom && i == 2) || (!left && i == 3)){
                continue;
            }
            CGPoint point1 = CGPointMake(basePoint1.x+arr1[i][0], basePoint1.y+arr1[i][1]);
            CGPoint point2 = CGPointMake(basePoint1.x+arr1[(i+1)%4][0], basePoint1.y+arr1[(i+1)%4][1]);
            for (int j=0;j < 4;j ++){
                if (i%2 == j%2){
                    CGPoint point3 = CGPointMake(basePoint2.x+arr2[j][0], basePoint2.y+arr2[j][1]);
                    CGPoint point4 = CGPointMake(basePoint2.x+arr2[(j+1)%4][0], basePoint2.y+arr2[(j+1)%4][1]);
                    if (i%2 == 0){
                        float vOffset = [self getVerticalOffset:point1 point2:point2 point3:point3 point4:point4 magnetLen:magnetLength];
                        if (fabs(vOffset) < fabs(minVerOffset)){
                            minVerOffset = vOffset;
                        }
                    }else{
                        float hOffset = [self getHorizonOffset:point1 point2:point2 point3:point3 point4:point4 magnetLen:magnetLength];
                        if (fabs(hOffset) < fabs(minHorOffset)){
                            minHorOffset = hOffset;
                        }
                    }
                }
            }
        }
    }
    
    minHorOffset = minHorOffset!=magnetLength?minHorOffset:0;
    minVerOffset = minVerOffset!=magnetLength?minVerOffset:0;
    return CGSizeMake(minHorOffset, minVerOffset);
}

please help me convert this code to C#, tks
Posted
Updated 9-Sep-20 22:41pm
v3

1 solution

 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900