- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
问题输入由三个点A, B, C
指定,每个点有两个坐标x, y
。
解决方案应该是具有自然坐标的三角形内所有点的数组。
输入是:A、B、C
输出是:图中所有命名点
请注意,我正在尝试计算所有的点而不是计算它们所以 this question和我的有很大不同。
我遇到的问题:
主要 问题是,指定所有三个段需要计算所有段的系数 a, b
这可能会扩展我的代码很多,因为我会必须涵盖所有水平线和垂直线的情况。
那么我能想到的最好的方法是:
x'es
,从点 A、B、C
的最小 x
到最大值。A、B、C
点的最小 y
到最大值迭代自然 y
。numpy
手动求解。大量的不平等是第二最困难的问题。一般来说,我能想到的任何方式都需要我编写很多代码,其中可能存在很多错误。此外,我编写的指令越多,性能就越低,因为使用了许多非平凡的计算方法。
如能提供更简单的解决方案,我们将不胜感激。
最佳答案
您可以找到通过每对点的线(线 AB、BC、AC)并检查这些线的哪一侧是三角形的内部。位于所有线的“内侧”侧的点都在三角形内:
def insidetriangle((x1,x2,x3),(y1,y2,y3)):
import numpy as np
xs=np.array((x1,x2,x3),dtype=float)
ys=np.array((y1,y2,y3),dtype=float)
# The possible range of coordinates that can be returned
x_range=np.arange(np.min(xs),np.max(xs)+1)
y_range=np.arange(np.min(ys),np.max(ys)+1)
# Set the grid of coordinates on which the triangle lies. The centre of the
# triangle serves as a criterion for what is inside or outside the triangle.
X,Y=np.meshgrid( x_range,y_range )
xc=np.mean(xs)
yc=np.mean(ys)
# From the array 'triangle', points that lie outside the triangle will be
# set to 'False'.
triangle = np.ones(X.shape,dtype=bool)
for i in range(3):
ii=(i+1)%3
if xs[i]==xs[ii]:
include = X *(xc-xs[i])/abs(xc-xs[i]) > xs[i] *(xc-xs[i])/abs(xc-xs[i])
else:
poly=np.poly1d([(ys[ii]-ys[i])/(xs[ii]-xs[i]),ys[i]-xs[i]*(ys[ii]-ys[i])/(xs[ii]-xs[i])])
include = Y *(yc-poly(xc))/abs(yc-poly(xc)) > poly(X) *(yc-poly(xc))/abs(yc-poly(xc))
triangle*=include
# Output: 2 arrays with the x- and y- coordinates of the points inside the
# triangle.
return X[triangle],Y[triangle]
在循环中解决了 3 个不等式,生成了 bool 数组,这些数组相乘后仅产生三角形内的点。
编辑:循环可以写得更不言自明:
for i in range(3):
ii=(i+1)%3
if xs[i]==xs[ii]:
if xc>xs:
include = (X > xs[i])
else:
include = (X < xs[i])
else:
slope=(ys[ii]-ys[i])/(xs[ii]-xs[i])
poly=np.poly1d([slope,ys[i]-xs[i]*slope])
if yc>poly(xc):
include = (Y > poly(X))
else:
include = (Y < poly(X))
triangle*=include
关于python - 确定三角形内的所有离散点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37181829/
如何在 Flutter 中使 slider 离散看起来像上图? slider discrete 最佳答案 使用 divisions Slider 的属性(property)小部件将其分成相等的部分,然
我正在创建一个带有颜色条的散点图 plt.scatter(X, Y, c=Z) plt.colorbar() plt.show() plt.close() 其中 X 和 Y 是 float 组,Z 是
我刚刚在 android studio 中发现了 seekbar 离散小部件,我发现它非常有用,但我不知道如何删除步骤指示器,或者用更合适的可绘制对象更改它们。 有人设法做到了吗? 这是我当前搜索栏的
问题 请注意以下问题:巫师可以创建和销毁 rune 。创建一个新的 rune 需要消耗与先前创建的 rune 数量成比例的法力。摧毁 rune 会恢复创建 rune 所用的法力。下面,我提出一个可能的
我正在尝试使用 ggplot2 中的 sf 和 geom_sf 制作 map ,其中一组点数据使用连续颜色比例(-1 到 1),一组线数据使用离散比例(a、b、c、d)着色。但是,当我在同一张 map
我正在尝试在具有连续状态(dim.= 20)和离散操作(3 个可能的操作)的环境中找到最佳策略。并且有一个特定的时刻:对于最佳策略,一个操作(称为“操作 0”)的选择频率应比其他两个操作高得多(频率约
仅使用 x-y 位置移动绘制圆弧的最佳方法是什么?例如,假设我想在点 (4,4) 处绘制一个半径为 4 的圆。让我们看看我的“抽屉”从 (4,0) 开始,每个方向的分辨率为 0.1 步。我将如何创建一
我正在使用一个使用广义加法模型的随机站点级效应来拟合一个模型,该模型在 mgcv 中实现。 R 包。我一直在使用函数 gam() 执行此操作但是,为了加快速度,我需要转到 bam()框架,与gam()
这个问题在这里已经有了答案: Make a line separated by group in bar chart (3 个答案) 关闭上个月。 我正在尝试使用 ggplot2 在条形图的每个条上
这个问题在这里已经有了答案: Make a line separated by group in bar chart (3 个答案) 关闭上个月。 我正在尝试使用 ggplot2 在条形图的每个条上
是否可以同时使用 Intel HD 4000 集成显卡和独立 GPU,OpenCL(或 CUDA)作为设备,CPU 作为主机?我想要一些代码在集成显卡上运行,而其他代码同时在我的 GPU 上运行。 最
我是一名优秀的程序员,十分优秀!