Я хотел бы вычислить угол между осью X системы отсчета и линией, вдоль которой больше всего развивается общий простой, выпуклый или вогнутый многоугольник.
На изображении я показал пример, где синяя линия представляет собой линию, вдоль которой многоугольник развивается больше всего.
Желтый кружок (извините, если вы его едва видите) представляет собой центроид, а красный кружок представляет собой середину многоугольника.
пример того, что я имею в виду
Я пытался решить проблему путем вычисления собственных векторов, связанных с наибольшим собственным значением ковариационной матрицы.
Это то, что я имею в виду? правильный способ решения проблемы или есть ли лучшие способы?
Я провел несколько тестов, и это, кажется, работает, но когда он пытается вычислить угол этого многоугольника, он возвращает значение около 80°, тогда как я ожидаю, что 0°.
неправильный результат
Это моя реализация на C#:
// Returns the angle between the x-axis and the line along which the figure extends mainly.
public double GetOrientation()
{
Point centroid = this.GetCentroid(); // Centroid of the polygon.
int vertexesNumber = this.GetVertexesNumber(); // Number of vertexes of the polygon.
double covXX = 0.0; // Variance of x, indicates how much x varies from the mean.
double covYY = 0.0; // Variance of y, indicates how much y varies from the mean.
double covXY = 0.0; // Covariance between x and y, indicates how much x and y vary together.
// Calculates the covariance matrix, indicates how the vertexes are distributed with respect to the centroid.
foreach (Point vertex in vertexes) // For each vertex of the polygon.
{
double x = vertex.X - centroid.X; // Calculates the difference of the vertex at x from the centroid at x.
double y = vertex.Y - centroid.Y; // Calculates the difference of the vertex at y from the centroid at y.
covXX += x * x; // Adds the square of the deviation from x to the centroid.
covYY += y * y; // Adds the square of the deviation from y to the centroid.
covXY += x * y; // Adds the product of deviations from x and y.
}
covXX /= vertexesNumber; // Calculates the mean of the variance along x.
covYY /= vertexesNumber; // Calculates the mean of the variance along y.
covXY /= vertexesNumber; // Calculates the mean of the covariance between x and y.
double tr = covXX + covYY; // Calculates the trace of the matrix.
double det = covXX * covYY - covXY * covXY; // Calculates the determinant of the matrix.
double lambda = (tr + Math.Sqrt(tr * tr - 4 * det)) / 2; // Calculates the greatest eigenvalue of the matrix.
double a1 = covXY; // Calculates the component along the x-axis of the eigenvector associated with the greatest eigenvalue.
double a2 = lambda - covXX; // Calculates the component along the y-axis of the eigenvector associated with the greatest eigenvalue.
double angle = Math.Atan2(a2, a1); // Calculates the angle between the x-axis and the line along which the polygon extends mainly, defined by the vector a1, a2.
angle *= 180.0 / Math.PI; // Converts the angle from radians to degrees.
return angle;
}
Подробнее здесь: https://stackoverflow.com/questions/791 ... -a-generic
Как рассчитать угол между осью X и линией, вдоль которой больше всего развивается общий многоугольник? ⇐ C#
Место общения программистов C#
1731496353
Anonymous
Я хотел бы вычислить угол между осью X системы отсчета и линией, вдоль которой больше всего развивается общий простой, выпуклый или вогнутый многоугольник.
На изображении я показал пример, где синяя линия представляет собой линию, вдоль которой многоугольник развивается больше всего.
Желтый кружок (извините, если вы его едва видите) представляет собой центроид, а красный кружок представляет собой середину многоугольника.
пример того, что я имею в виду
Я пытался решить проблему путем вычисления собственных векторов, связанных с наибольшим собственным значением ковариационной матрицы.
Это то, что я имею в виду? правильный способ решения проблемы или есть ли лучшие способы?
Я провел несколько тестов, и это, кажется, работает, но когда он пытается вычислить угол этого многоугольника, он возвращает значение около 80°, тогда как я ожидаю, что 0°.
неправильный результат
Это моя реализация на C#:
// Returns the angle between the x-axis and the line along which the figure extends mainly.
public double GetOrientation()
{
Point centroid = this.GetCentroid(); // Centroid of the polygon.
int vertexesNumber = this.GetVertexesNumber(); // Number of vertexes of the polygon.
double covXX = 0.0; // Variance of x, indicates how much x varies from the mean.
double covYY = 0.0; // Variance of y, indicates how much y varies from the mean.
double covXY = 0.0; // Covariance between x and y, indicates how much x and y vary together.
// Calculates the covariance matrix, indicates how the vertexes are distributed with respect to the centroid.
foreach (Point vertex in vertexes) // For each vertex of the polygon.
{
double x = vertex.X - centroid.X; // Calculates the difference of the vertex at x from the centroid at x.
double y = vertex.Y - centroid.Y; // Calculates the difference of the vertex at y from the centroid at y.
covXX += x * x; // Adds the square of the deviation from x to the centroid.
covYY += y * y; // Adds the square of the deviation from y to the centroid.
covXY += x * y; // Adds the product of deviations from x and y.
}
covXX /= vertexesNumber; // Calculates the mean of the variance along x.
covYY /= vertexesNumber; // Calculates the mean of the variance along y.
covXY /= vertexesNumber; // Calculates the mean of the covariance between x and y.
double tr = covXX + covYY; // Calculates the trace of the matrix.
double det = covXX * covYY - covXY * covXY; // Calculates the determinant of the matrix.
double lambda = (tr + Math.Sqrt(tr * tr - 4 * det)) / 2; // Calculates the greatest eigenvalue of the matrix.
double a1 = covXY; // Calculates the component along the x-axis of the eigenvector associated with the greatest eigenvalue.
double a2 = lambda - covXX; // Calculates the component along the y-axis of the eigenvector associated with the greatest eigenvalue.
double angle = Math.Atan2(a2, a1); // Calculates the angle between the x-axis and the line along which the polygon extends mainly, defined by the vector a1, a2.
angle *= 180.0 / Math.PI; // Converts the angle from radians to degrees.
return angle;
}
Подробнее здесь: [url]https://stackoverflow.com/questions/79184598/how-to-calculate-the-angle-between-the-x-axis-and-the-line-along-which-a-generic[/url]
Ответить
1 сообщение
• Страница 1 из 1
Перейти
- Кемерово-IT
- ↳ Javascript
- ↳ C#
- ↳ JAVA
- ↳ Elasticsearch aggregation
- ↳ Python
- ↳ Php
- ↳ Android
- ↳ Html
- ↳ Jquery
- ↳ C++
- ↳ IOS
- ↳ CSS
- ↳ Excel
- ↳ Linux
- ↳ Apache
- ↳ MySql
- Детский мир
- Для души
- ↳ Музыкальные инструменты даром
- ↳ Печатная продукция даром
- Внешняя красота и здоровье
- ↳ Одежда и обувь для взрослых даром
- ↳ Товары для здоровья
- ↳ Физкультура и спорт
- Техника - даром!
- ↳ Автомобилистам
- ↳ Компьютерная техника
- ↳ Плиты: газовые и электрические
- ↳ Холодильники
- ↳ Стиральные машины
- ↳ Телевизоры
- ↳ Телефоны, смартфоны, плашеты
- ↳ Швейные машинки
- ↳ Прочая электроника и техника
- ↳ Фототехника
- Ремонт и интерьер
- ↳ Стройматериалы, инструмент
- ↳ Мебель и предметы интерьера даром
- ↳ Cантехника
- Другие темы
- ↳ Разное даром
- ↳ Давай меняться!
- ↳ Отдам\возьму за копеечку
- ↳ Работа и подработка в Кемерове
- ↳ Давай с тобой поговорим...
Мобильная версия