Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Collision #2

Open
bubudrc opened this issue Jun 13, 2017 · 3 comments
Open

Collision #2

bubudrc opened this issue Jun 13, 2017 · 3 comments

Comments

@bubudrc
Copy link

bubudrc commented Jun 13, 2017

Hello,

Before all, I love your project.

I'm trying to modify this, because I need create a shape based on line collisions. I mean, If two lines collision, I must join them in the vertex. If, I join more than two line, created a close shape, I must create that shape.

Do you know where can I take a look on that? using your project?

@lll1024
Copy link
Owner

lll1024 commented Jun 14, 2017

Thank you,

I don't know if you need the shape was established through linear or graffiti, if it is a straight line, I think it's not difficult to your needs; All shapes was established based on the coordinate, when the two straight lines intersect, you need to decide which should connect the other lines at one end, usually connect one end of the endpoint closer.If the connection point overlaps the starting point, you finished this shape which you can trying to edit, move and even cancel it.

I'm sorry. I did not realize this function. If I have free time I will trying to do it.

@bubudrc
Copy link
Author

bubudrc commented Jun 14, 2017

Hi, thanks for your answer.

Ok, I will try... if I cant do it, I will wait you... If I can, I going to update the fork :D

@bubudrc
Copy link
Author

bubudrc commented Jun 15, 2017

This is what I have:

- (void)touchesEnded:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
    if (![self.layerArray containsObject:self.drawingLayer] && !self.isFirstTouch) {
        [self.layerArray addObject:self.drawingLayer];
        [self.drawingLayer addToTrack];
    } else {
        if (self.isMoveLayer) {
            [self.selectedLayer addToTrack];
        }
        if (self.isFirstTouch) {
            self.selectedLayer.isSelected = NO;
            self.selectedLayer = nil;
            
            UITouch *touch = [touches anyObject];
            CGPoint currentPoint = [touch locationInView:self];
            for (JVDrawingLayer *layer in self.layerArray) {
                if ([layer caculateLocationWithPoint:currentPoint]) {
                    self.selectedLayer = layer;
                    self.selectedLayer.isSelected = YES;
                    [self.layerArray removeObject:self.selectedLayer];
                    [self.layerArray addObject:self.selectedLayer];
                    break;
                }
            }
            
            self.drawingLayerSelectedBlock(self.selectedLayer);
        } else {
            
            NSInteger index = [self.layerArray indexOfObject:self.selectedLayer];

            [self.layerArray enumerateObjectsUsingBlock:^(JVDrawingLayer *layer, NSUInteger idx, BOOL * _Nonnull stop) {
                if(idx != index) {
                    if([self distanceBetween:layer.startPoint and:self.selectedLayer.startPoint] <= 10 ||
                       [self distanceBetween:layer.startPoint and:self.selectedLayer.endPoint] <= 10 ||
                       [self distanceBetween:layer.endPoint and:self.selectedLayer.startPoint] <= 10 ||
                       [self distanceBetween:layer.endPoint and:self.selectedLayer.endPoint] <= 10){
                        NSLog(@"Intersection: %@", layer);
                        
                        *stop = YES;
                        return;
                    }
                }
            }];
        }
    }
}

- (float)distanceBetween:(CGPoint)p1 and:(CGPoint)p2
{
    return sqrt(pow(p2.x-p1.x,2)+pow(p2.y-p1.y,2));
}

What do you think? do you have a better way to do it?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants