- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我想创建无限重复的线条图案。然而,线在水平和垂直方向上都从线的末端偏移了一些定义的间距。如下图所示:
现在我想要这个图案在水平和垂直方向上都有偏移。模式的使用应如图 2 所示。
现在,我正在手动执行三 Angular 函数以创建给定矩形宽度和高度的线,但我想利用 <pattern>
在 SVG 中。没有<pattern>
我很难改变渲染图案的比例。
不幸的是,我无法从任何在线教程或引用文档中找到进行偏移重复的方法。
我尝试过的:
这是我到目前为止所做的,给定 S、线的长度、X 偏移量、Y 偏移量
现在通过在顶部方向传播偏移量来重复 (1,4)。如果偏移后,您在框外:- 绘制一条非常大的线,如果它与框相交:从交点开始绘制。
重复直到 2 组偏移不与框相交。我用 python 编写了所有这些并创建了 <line>
.
不幸的是,我无法缩放它,而且我无法让矩形填充图案大小的变化。我正在为简单模式渲染 100 行。
这是我的python代码:
def cosd(angle):
return np.cos(angle*np.pi/180.0)
def sind(angle):
return np.sin(angle*np.pi/180.0)
class pattern():
def __init__(self, angle, x, y, x_offset, y_offset, dashes):
self.angle = angle
self.p = Point(x, y)
self.x_offset = x_offset
self.y_offset = y_offset
self.dashes = dashes
self.svg_lines = []
# Check if the origins are in the box
def in_box(self, poly):
if poly.encloses_point(self.p) == False:
if len(intersection(self.p, poly)) < 1:
return False
else:
return True
else:
return True
def offset_me(self, direction='+'):
"""
x offset creates vector U in direction of angle of Length = x_offset
y offset creates vector V in direction perp of U with length of y_offset
Adding these two vector provides new offset location
"""
x1 = self.p[0]
y1 = self.p[1]
angle = self.angle
theta = 90 - angle
if direction == '-':
theta = -1*theta
x3 = x1 + cosd(angle)*self.x_offset + cosd(theta)*self.y_offset
y3 = y1 + sind(angle)*self.x_offset + sind(theta)*self.y_offset
self.p = Point(x3, y3)
print "Offsetting in " + direction
def draw_svg_line(self, p1, p2):
print Segment(p1, p2)
self.svg_lines.append(Segment(p1, p2))
def clean_report_svg_lines(self,poly):
rep = self.svg_lines
result = []
for item in rep:
if len(intersection(item,poly)) > 0 or poly.encloses_point(item.points[0]):
if item not in result:
result.append(item)
txt = []
for item in result:
x1 = item.points[0][0]
y1 = item.points[0][1]
x2 = item.points[1][0]
y2 = item.points[1][1]
svg_str = '<line x1="{}" y1="{}" x2="{}" y2="{}" style="stroke:rgb(255,0,0);stroke-width:0.5" />'.format(x1,y1,x2,y2)
txt.append(svg_str)
return txt
def draw_all(self, poly):
# Draw in Positive Direction
starter_point = self.p.copy()
#self.draw_me(poly, direction='+')
while poly.encloses_point(self.p):
self.draw_me(poly, direction='+')
self.p = starter_point
starter_point = self.p.copy()
#self.draw_me(poly, direction='-')
while poly.encloses_point(self.p):
self.draw_me(poly, direction='-')
def draw_me(self, poly, direction='+'):
angle = self.angle
# Draw backwards
my_dash = self.dashes
if direction == '-':
my_dash.reverse()
for L in self.dashes:
abs_L = abs(L)
if L > 0:
if direction == '-':
x2 = self.p[0] - cosd(angle)*abs_L
y2 = self.p[1] - sind(angle)*abs_L
else:
x2 = self.p[0] + cosd(angle)*abs_L
y2 = self.p[1] + sind(angle)*abs_L
line = Segment(self.p, Point(x2,y2))
inter = intersection(line, poly)
self.draw_svg_line(self.p,Point(x2,y2))
self.p = Point(x2,y2)
elif L == 0:
pass
elif L < 0:
if direction == '-':
self.p = Point(self.p[0] - abs_L*cosd(angle), self.p[1] - abs_L*sind(angle))
else:
self.p = Point(self.p[0] + abs_L*cosd(angle), self.p[1] + abs_L*sind(angle))
def print_me(self):
print self.p
print self.angle
def travel(self,p,L, direction='+'):
angle = self.angle
x2 = p[0] + cosd(angle)*L
y2 = p[1] + sind(angle)*L
if direction == '-':
x2 = p[0] - cosd(angle)*L
y2 = p[1] - sind(angle)*L
p2 = Point(x2,y2)
return p2
def get_starting_point(self, poly):
# Its already in box
if self.in_box(poly):
return True, self.p
length = sum([abs(x) for x in self.dashes])
p2 = self.travel(self.p, length)
if poly.encloses_point(p2):
return True, p2
if intersection(Line(self.p, p2),poly):
# It has intersections but which way?
i = intersection(Line(self.p, p2),poly)
print type(i[0])
if isinstance(i[0], sympy.geometry.point.Point2D):
i = Segment(i[0], i[1])
else:
i = i[0]
p_midpoint = i.midpoint
print p_midpoint[0]
direction = '+'
if p_midpoint[0] - self.p[0] <= 0:
direction = '-'
# Now we know which way it is go back until we hit....
while len(intersection(Segment(self.p, p2),poly)) < 1:
self.p = self.travel(self.p, length,direction)
p2 = self.travel(self.p, length,direction)
return True, p2
else:
return False, None
# Get origins to the box
def offset_draw(self, poly):
starting_p = self.p.copy()
possible, self.p = self.get_starting_point(poly)
self.draw_all(poly)
# Positive Draw
while possible:
self.draw_all(poly)
self.offset_me(direction='+')
possible, self.p = self.get_starting_point(poly)
self.p = starting_p
print self.p
self.offset_me(direction='-')
possible, self.p = self.get_starting_point(poly)
# Negetive Draw
while possible:
self.draw_all(poly)
self.offset_me(direction='-')
possible, self.p = self.get_starting_point(poly)
我将 python 代码(行元素)的粘贴结果复制到 svg 模式标签中: Codepen example
最佳答案
既然你标记了这个 d3
,我假设 d3 答案是可以接受的?无论如何,它将向您展示如何构建模式并且应该可以轻松转换为 python
:
<!DOCTYPE html>
<html>
<head>
<script data-require="d3@4.0.0" data-semver="4.0.0" src="https://d3js.org/d3.v4.min.js"></script>
</head>
<body>
<script>
var width = 1000,
height = 1000,
offsetX = 25,
offsetY = 25,
lineLength = 75,
strokeWidth = 3;
var svg = d3.select('body')
.append('svg')
.attr('width', width)
.attr('height', height);
var p = svg.append('pattern')
.attr('id', 'myPattern')
.attr('x', 0)
.attr('y', 0)
.attr('width', (offsetX + lineLength) / width)
.attr('height', (offsetY * 2) / height);
p.append("line")
.attr("x1", 0)
.attr("x2", lineLength)
.attr("y1", strokeWidth)
.attr("y2", strokeWidth)
.style("stroke-width", strokeWidth)
.style("stroke", "black");
p.append("line")
.attr("x1", offsetX)
.attr("x2", lineLength + offsetX)
.attr("y1", offsetY + strokeWidth)
.attr("y2", offsetY + strokeWidth)
.style("stroke-width", strokeWidth)
.style("stroke", "black");
p.append("circle")
.attr("cx", lineLength)
.attr("cy", strokeWidth)
.attr("r", strokeWidth);
p.append("circle")
.attr("cx", strokeWidth)
.attr("cy", strokeWidth)
.attr("r", strokeWidth);
p.append("circle")
.attr("cx", offsetX)
.attr("cy", offsetY + strokeWidth)
.attr("r", strokeWidth);
p.append("circle")
.attr("cx", lineLength + offsetX - strokeWidth)
.attr("cy", offsetY + strokeWidth)
.attr("r", strokeWidth);
svg.append("rect")
.attr("width", width)
.attr("height", height)
.attr("fill","url(#myPattern)");
</script>
</body>
</html>
关于html - 创建无限平行模式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47847713/
这个问题在这里已经有了答案: Can I get `cabal install` to use multiple cores? (3 个回答) 关闭 7 年前。 在使用类似于 GNU make 的 -
我正在尝试通过 akeeba backup 在 parallels plesk 面板中安装一个 joomla 站点。我在哪里面临文件权限问题。 An error occured Could not o
我在 MATLAB 中使用箱线图函数。我需要为 6 个“XTicks”绘制 6 个不同数据集的箱线图,即 x 轴上的每个刻度线应包含 6 个相应的框、晶须、中线和其域内的异常值集。我尝试通过为每个变量
我需要在 Kaplan Meier 图上呈现 at_risk 数字。 最终结果应该与此类似: 我在渲染时遇到的问题是 No。处于危险中的患者数量位于图表底部。此处显示的值对应于 x 轴上的值。因此本质
我想知道你们中的任何一个人为什么知道我的表现糟透了吗? 我正在努力实现的目标; 生成220万个文件。要创建每个文件,平均需要2-5个数据库调用。 我正在使用的服务器具有24个内核和190GB的RAM。
请帮忙。我正在研究具有此要求的算法。 给定 4 个“右”矩形(右矩形的边平行于 x 或 y),找出它们中的任何一个覆盖的区域 例如,灰色区域被下图中的 4 个矩形中的任何一个覆盖。 enter ima
我是一名优秀的程序员,十分优秀!