All Downloads are FREE. Search and download functionalities are using the official Maven repository.

darwin.util.math.composits.Circle Maven / Gradle / Ivy

/*
 * Copyright (C) 2012 daniel
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see .
 */
package darwin.util.math.composits;

import darwin.util.math.base.Line;
import darwin.util.math.base.vector.*;

import static java.lang.Math.sqrt;

/**
 *
 * @author daniel
 */
public class Circle {

    private final ImmutableVector center;
    private final float radius;

    public Circle(ImmutableVector center, float radius) {
        this.center = center;
        this.radius = radius;
    }

    public Circle getScaledCircle(float scale) {
        return new Circle(center, radius * scale);
    }

    public Circle getExtrudedCircle(float amount) {
        return new Circle(center, Math.abs(radius - amount));
    }

    public float getRadius() {
        return radius;
    }

    public ImmutableVector getCenter() {
        return center;
    }

    /**
     * @param line < p/>
     * 

* @return null if the line does not intersect with the circle */ public LineSegment getIntersection(Line line) { assert !contains(line.getStartingPoint()); Vector2 toCircle = center.clone().sub(line.getStartingPoint()); float distSquare = (float) toCircle.lengthQuad(); if (distSquare == 0) { toCircle = center.clone().sub(line.getStartingPoint().clone().add(line.getDirection())); distSquare = (float) toCircle.lengthQuad(); } float t = toCircle.dot(line.getDirection()); float tmp = radius * radius - distSquare + t * t; if (tmp < 0) { return null;//no intersection } float f = (float) sqrt(tmp); return new LineSegment<>(line.getDirection().clone().mul(t - f).add(line.getStartingPoint()), line.getDirection().clone().mul(t + f).add(line.getStartingPoint())); } public boolean intersectsWith(LineSegment segment) { float r2 = radius * radius; return (segment.asLine().distanceToSquared(center) < r2 && segment.isInsideInterval(center)) || center.distanceQuad(segment.getStart()) < r2 || center.distanceQuad(segment.getEnd()) < r2; } public boolean contains(ImmutableVector point) { return point.clone().sub(center).length() <= radius; } }





© 2015 - 2025 Weber Informatics LLC | Privacy Policy