UIView coordinate systems - offset and translation

I’m sure this is more of a mathematical question, but I will formulate it in the context of UIView and the iPad-related goal of C

I am importing raw data from a mapping file that I created from some public domain material loaded in another location, and then split it to isolate different areas on the map. Each region has several subregions, such as the continental United States, and then various states that appear in the United States, and then each subregion is again divided into, say, counties.

Each state and each county has a bounding box that tells me where the origin, width and height come from.

In my initial setup, I created a separate view for each state, and then a different view for each county. The polygon representing the state / county area was visualized (obviously with a county on top of the state so that it was visible) relative to the view I created through the interface builder called mainContainerView. This initial setup worked correctly.

Now I'm trying to make a little difference by adding counties to the UIView while holding the polygon for the state, so I can apply the state as a clipping mask in the counties. The problem is that no matter what I try, I can not get the county to transfer to the right place in the state view.

It seems like it should be simple addition or subtraction, since the scaling for each element is exactly the same, and I am not trying to do any major transformations, so I do not think that the CFAffineTransformation family is necessary.

I can send the code if necessary, but I'm not trying to get someone to write my program for me; I just want someone to point me in the right direction here, offering me a suggestion on how to establish a county regarding the state in the state’s representation.

, , . , , . , , .SHP, ( ). , , , .

MASK_MAX_EASTING, MASK_MAX_NORTHING, MASK_MIN_EASTING MASK_MIN_NORTHING - , , .

DIST_MAX_EASTING, DIST_MAX_NORTHING, DIST_MIN_EASTING DIST_MIN_NORTHING - , , . , , , .

-(void)didLoadMap:(NSNotification *)notification {

id region = [notification object];
ShapePolyline *polygon = [region polygon];

if ([notification name] == @"MapsLoadingForState") {

    // m_nBoundingBox is an array which contains the RAW northing and easting values for each subdivision.  [0] - west limit, [1] - south limit, [2] - east limit, [3] - north limit.

    // The code below, combined with the drawrect method in DrawMap.m (below) puts all the states on the map in precisely the right places, so for the state maps, it works just fine.

    CGFloat originX = ((polygon->m_nBoundingBox[0]-MASK_MIN_EASTING)*stateScaleMultiplier)+([mainContainerView frame].size.width/2);

    CGFloat originY = ((MASK_MAX_NORTHING-(polygon->m_nBoundingBox[3]))*stateScaleMultiplier)+[mainContainerView frame].origin.y;  
    CGFloat width = polygon->m_nBoundingBox[2] - polygon->m_nBoundingBox[0];
    CGFloat height = polygon->m_nBoundingBox[3] - polygon->m_nBoundingBox[1];

    CGFloat scaledWidth = width*stateScaleMultiplier;
    CGFloat scaledHeight = height*stateScaleMultiplier;
    UIColor *subViewColor = [UIColor colorWithRed:0.0 green:1.0 blue:1.0 alpha:0.0];

    stateMapView = [[DrawMap alloc] initWithFrame:CGRectMake(originX, originY, scaledWidth, scaledHeight)];
    [stateMapView setBackgroundColor:subViewColor];

    [stateMapView setStateScale:stateScaleMultiplier];
    [stateMapView setCountyScale:countyScaleMultiplier];  // Not actually needed.

    [stateMapView setClippingMask:polygon];
    UIColor *colorMask = [UIColor colorWithWhite:1.0 alpha:1.0];
    [stateMapView setForeground:colorMask];

    [states addObject:stateMapView];                      // Add the state map view to an array (for future use)
    [mapView addSubview:stateMapView];  // MapView is a UIView of equivalent size and shape as mainContainerView.

} else {

    // This is where the problems occur.  

    CGFloat originX = (polygon->m_nBoundingBox[0]-DIST_MIN_EASTING);  // 4431590 (raw data)
    originX *= countyScaleMultiplier;  // 303.929108
    originX += ([mainContainerView frame].size.width/2);  // 815.929077

    CGFloat originY = (DIST_MAX_NORTHING-polygon->m_nBoundingBox[3]); 4328997 
    originY *= countyScaleMultiplier;  // 296.893036
    originY -= [mainContainerView frame].origin.y;  // 340.893036

    CGRect frame = [stateMapView frame];  // Dummy variable created for watches in the debugger.  x=856.237183, y=332.169922 width=34.3800087, height=28.7534008

    // When I was invoking DrawMap.h and the included drawrect method, the county map would easily be displayed in the right place, as you can see by the values above.

    // This is where I think the problem is.  The X value is WAY off as far as I can tell.
    originX -= frame.origin.x; // -40.3081055 
    originY -= frame.origin.y; // 8.72311401

    CGPoint countyOrigin = CGPointMake(originX,originY);

    // Translate the county origin so it is relative to the origin of stateMapView, not MainContainerView (doesn't work)

    [stateMapView addCountyMap:[region polygon] withColor:winner translatedBy:countyOrigin];
    [stateMapView setNeedsDisplay];
}

, , , , ( ), ...

DrawMap.m; , .

- (void)drawRect:(CGRect)rect {

// Set up

for (int i=0;i<[countyMaps count];i++) {

    // Draw the polygon.

    [[countyColors objectAtIndex:i] setFill];

    [self drawPolygon:[countyMaps objectAtIndex:i]
           usingScale:stateScale
         translatedBy:CGPointMake([[countyTranslations objectAtIndex:2*i] floatValue],
                                  [[countyTranslations objectAtIndex:2*i+1] floatValue])]; 

}

// Set the blend mode to multiply

CGContextSetBlendMode(context, kCGBlendModeMultiply);

// Draw a path with clippingMask

[[UIColor colorWithWhite:1.0 alpha:1.0] setFill];
// CGPoint translate = CGPointMake(0,0);

[self drawPolygon:clippingMask usingScale:stateScale translatedBy:CGPointMake(0,0)];

}

-(void)drawPolygon:(ShapePolyline *)aPolygon usingScale:(float)mapScale translatedBy:(CGPoint)trans {

for (int j=0;j<[aPolygon numParts];j++) {

    UIBezierPath *path = [UIBezierPath bezierPath];

    [path setLineJoinStyle:kCGLineJoinRound];

    int startIndex = [[[aPolygon m_Parts] objectAtIndex:j] intValue];
    int endIndex = [aPolygon numPoints];

    CGPoint startPoint;
    [[[aPolygon m_Points] objectAtIndex:startIndex] getValue:&startPoint];

    startPoint.x *=mapScale;
    startPoint.y *=mapScale;

    startPoint.x -= trans.x;
    startPoint.y -= trans.y;

    [path moveToPoint:startPoint];


    if (j+1 != [aPolygon numParts]){ 

        endIndex = [[[aPolygon m_Parts] objectAtIndex:j+1] intValue];
    }

    for (int k=startIndex+1; k<endIndex; k++)
    {
        CGPoint nextPoint;
        [[[aPolygon m_Points] objectAtIndex:k] getValue:&nextPoint];

        nextPoint.x *= mapScale;
        nextPoint.y *= mapScale;

        nextPoint.x -= trans.x;
        nextPoint.y -= trans.y;

        [path addLineToPoint:nextPoint];

    }
    [path closePath];
    // [path stroke];
    [path fill];
}

}

, . , , , , ...

+3
1

-SOLVED -

. , , , - :

, . , 44 Y ( ).

, , , , , , , , " !!!!"

+1

All Articles