发布于2024-12-12 12:25 阅读(1002) 评论(0) 点赞(25) 收藏(5)
I have a set of vertices that form a curve in x and y. The curve is smooth, not convex, doesn't intersect and there are no vertical or beyond vertical parts. The x values are unique integers. The picture below illustrates an exemplary curve. You can also see the point light source above the curve. This light is always well above the curve.
Now I want to calculate which of the vertices are shadowed by the mesh. If possible I want to avoid casting rays and calculating where they possibly intersect with the mesh. Since the mesh is a simple line I'm guessing there is an easier way to find the shadowed vertices.
What I tried is calculating the angle between the y-axis and the ray from the light to each vertex with angle = arctan((y_point - y[i]), (x_point - x[i]))
. This represents the angle between a ray to the left of the light source to each vertex. I Have drawn the rays in the image below. As you can see, the points at x==[3,5,21,22,23]
should be shadowed, that's why I have manually coloured them green.
Now if the curve was flat, for each point of increasing x the angle calculated above should also be increasing. If it however decreases compared to the previous point, either this point or the previous point are in shadow. The results from this are shown below:
As you can see it correctly detects vertices at x==[21,22]
, but it misses x==23
and it only detects the neighbours of the shadowed vertices on the left.
For x==23
I thought I could calculate the cummax(angle)
for each point and compare the point's angle to this cummax angle. If angle < cummax(angle)
then that point is shadowed. This now also catches the vertex at x==23
, but still fails on the left side.
How can I fix the left side?
I'm working with python but this should apply to any language. If possible I want to avoid casting rays and calculating every edge where the ray could intersect the curve. If you want to replicate the curve the coordinates are
"x": [ 1, 2, 3, 4, 5,6,7,8,13,18,19,20,21,22,23,24,]
"y": [-2,-3,-10,-1,-2,2,3,4, 7, 5, 5, 5, 3, 1, 1, 1,]
"point": (11, 16)
Following your own idea, you just need to check the left and right sides of the light separately:
import matplotlib.pyplot as plt
import numpy as np
x = np.array([ 1, 2, 3, 4, 5,6,7,8,13,18,19,20,21,22,23,24,])
y = np.array([-2,-3,-10,-1,-2,2,3,4, 7, 5, 5, 5, 3, 1, 1, 1,])
point = (11, 16)
xp, yp = point
n = len(x)
# find the "index" of the light point: xp will be between x[ip-1] and x[ip]
for ip in range(n):
if x[ip] >= xp:
break
angles = np.arctan2(yp-y, xp-x)
cummax = np.maximum.accumulate
cummin = np.minimum.accumulate
shadow_R = angles[ip:] < cummax(angles[ip:])
shadow_L = (angles[ip:None:-1] > cummin(angles[ip:None:-1]))[::-1]
shadow = np.hstack((shadow_L, shadow_R))
plt.figure(1).clf()
plt.plot(x, y, 'o-b')
plt.plot(*point, 'o')
for xi,yi,s in zip(x,y,shadow):
s = 'r:' if s else 'g'
plt.plot([xi,xp], [yi,yp], s)
作者:黑洞官方问答小能手
链接:https://www.pythonheidong.com/blog/article/2046528/50d20b8f76b32bbc4e8a/
来源:python黑洞网
任何形式的转载都请注明出处,如有侵权 一经发现 必将追究其法律责任
昵称:
评论内容:(最多支持255个字符)
---无人问津也好,技不如人也罢,你都要试着安静下来,去做自己该做的事,而不是让内心的烦躁、焦虑,坏掉你本来就不多的热情和定力
Copyright © 2018-2021 python黑洞网 All Rights Reserved 版权所有,并保留所有权利。 京ICP备18063182号-1
投诉与举报,广告合作请联系vgs_info@163.com或QQ3083709327
免责声明:网站文章均由用户上传,仅供读者学习交流使用,禁止用做商业用途。若文章涉及色情,反动,侵权等违法信息,请向我们举报,一经核实我们会立即删除!