[Python] A simple function to find the center coordinates of a circle

A simple function to find the center coordinates of a circle

This is a simple method of selecting three points in order from the array, finding two perpendicular bisectors, and centering the intersection of them.

import numpy as np
import matplotlib.pyplot as plt
import random

#Total number of points
N = 4

#xy is input, xy_o outputs
#Both are two-dimensional arrays and shapes,[[x1, y1], [x2, y2], [x3, y3], ...]
def circle_c(xy):
    #Since 3 points are used to find the center of the circle, the output is reduced by 2 points.
    xy_o_len = len(xy) - 2
    #Set the array for output.
    xy_o = np.zeros((xy_o_len, 2))
    #Find the center of the circle by 3 points in order from the front.
    for i in range(xy_o_len):
        #Auxiliary calculation
        x12 = xy[i+0, 0] + xy[i+1, 0]
        # x13 = xy[i+0, 0] + xy[i+2, 0]
        x23 = xy[i+1, 0] + xy[i+2, 0]
        x21 = xy[i+1, 0] - xy[i+0, 0]
        # x31 = xy[i+2, 0] - xy[i+0, 0]
        x32 = xy[i+2, 0] - xy[i+1, 0]
        y12 = xy[i+0, 1] + xy[i+1, 1]
        # y13 = xy[i+0, 1] + xy[i+2, 1]
        # y23 = xy[i+1, 1] + xy[i+2, 1]
        y21 = xy[i+1, 1] - xy[i+0, 1]
        y31 = xy[i+2, 1] - xy[i+0, 1]
        y32 = xy[i+2, 1] - xy[i+1, 1]
        #Calculate the center of the circle
        xy_o[i, 0] = (y31 - x21*x12/y21 + x32*x23/y32)*0.5 / (x32/y32 - x21/y21) #x component
        xy_o[i, 1] = -(xy_o[i, 0] - x12 * 0.5)*x21/y21 + y12*0.5 #y component
    return xy_o

x = np.arange(N)
y = np.random.rand(N)
xy = np.c_[x, y]

cxy = circle_c(xy)

f = plt.subplot()
f.plot(xy[:, 0], xy[:, 1], marker='.', markersize=20)
f.scatter(cxy[:, 0], cxy[:, 1], s=900, c="pink", alpha=0.5, linewidths="2", edgecolors="red")
f.set_aspect('equal')
plt.show()

Recommended Posts

[Python] A simple function to find the center coordinates of a circle
[Python3] Define a decorator to measure the execution time of a function
Get the caller of a function in Python
Python: I want to measure the processing time of a function neatly
I made a function to see the movement of a two-dimensional array (Python)
Python Note: The mystery of assigning a variable to a variable
Add a function to tell the weather of today to slack bot (made by python)
I tried to find the entropy of the image with python
Find out the apparent width of a string in python
A simple Python implementation of the k-nearest neighbor method (k-NN)
Write a python program to find the editing distance [python] [Levenshtein distance]
I made a function to check the model of DCGAN
How to find the scaling factor of a biorthogonal wavelet
Find the eigenvalues of a real symmetric matrix in Python
[Python] Programming to find the number of a in a character string that repeats a specified number of times.
I made a function to crop the image of python openCV, so please use it.
[Python] A program to find the number of apples and oranges that can be harvested
How to determine the existence of a selenium element in Python
Find the optimal value of a function with a genetic algorithm (Part 2)
[Introduction to Python] How to split a character string with the split function
A story that struggled to handle the Python package of PocketSphinx
How to check the memory size of a variable in Python
How to check the memory size of a dictionary in Python
How to find the memory address of a Pandas dataframe value
How to get the vertex coordinates of a feature in ArcPy
Create a function to get the contents of the database in Go
[python] A note that started to understand the behavior of matplotlib.pyplot
[Python] A program that rotates the contents of the list to the left
Find the intersection of a circle and a straight line (sympy matrix)
[Python3] Rewrite the code object of the function
A simple IDAPython script to name a function
[python] [meta] Is the type of python a type?
Find the white Christmas rate by prefecture with Python and map it to a map of Japan
The story of blackjack A processing (python)
Have Alexa run Python to give you a sense of the future
[Python] Explains how to use the range function with a concrete example
Various methods to numerically create the inverse function of a certain function Introduction
[Introduction to Python] How to sort the contents of a list efficiently with list sort
How to create a wrapper that preserves the signature of the function to wrap
[Introduction to Python] How to write a character string with the format function
Find the minimum value of a function by particle swarm optimization (PSO)
Python code to determine the monthly signal of a relative strength investment
I made a program to check the size of a file in Python
[Python] Note: A self-made function that finds the area of the normal distribution
Introduction and usage of Python bottle ・ Try to set up a simple web server with login function
How to calculate the volatility of a brand
Draw a graph of a quadratic function in Python
A simple example of how to use ArgumentParser
How to find the area of the Voronoi diagram
Combinatorial optimization to find the hand of "Millijan"
To execute a Python enumerate function in JavaScript
Make a copy of the list in Python
Find the number of days in a month
A note about the python version of python virtualenv
Find the divisor of the value entered in python
Find the solution of the nth-order equation in python
[Python] A rough understanding of the logging module
Output in the form of a python array
Find the geometric mean of n! Using Python
A python amateur tries to summarize the list ②
[Python] Find the transposed matrix in a comprehension