斜纹的代码错了,请重新编写,竹编的用六边形竹编,也重新写一下
- User
斜纹的代码错了,请重新编写,竹编的用六边形竹编,也重新写一下
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
斜纹的代码错了,请重新编写,竹编的用六边形竹编,也重新写一下
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
好的,我已经修改了斜纹(Twill Weave)的代码,使其能更清晰地展示2/1斜纹(两上一下)的结构,并为竹编(Bamboo Weave)提供了一个基于六边形单元的编织可视化。
关于斜纹的修正: 之前的斜纹代码可能在视觉上没有很好地体现“浮长”的概念。新的代码会更明确地在经纬线交点处显示纬线是在经线之上还是之下,并形成清晰的斜向纹理。
关于六边形竹编: 六边形竹编(有时也称为三角编织或 Kagome 编织)是一种常见的竹编结构,由三个方向的竹条交织而成。这里我们用三组不同颜色的平行线(模拟竹条)以60度角相互交叉来表现。
以下是更新后的 Matlab 代码:
% --- 清理工作区和图形 ---
close all; clear; clc;
grid_size = 10; % 定义网格大小
% --- 1. 平纹 (Plain Weave) ---
figure('Name', 'Plain Weave');
hold on;
axis equal;
axis([0 grid_size 0 grid_size]);
title('平纹织法 (Plain Weave)');
xlabel('经线 (Warp - Blue)'); ylabel('纬线 (Weft - Red)');
% 经线 (蓝色)
for i = 1:grid_size
plot([i i], [0 grid_size], 'b-', 'LineWidth', 1.5);
end
% 纬线 (红色)
for j = 1:grid_size
for i = 1:grid_size
if mod(i+j, 2) == 0
% 纬线在经线之上
plot([i-0.5 i+0.5], [j j], 'r-', 'LineWidth', 2);
else
% 纬线在经线之下 (用颜色和线宽稍作区分,或不画此段)
% 为了清晰,这里我们只画“在上”的部分,经线本身代表了“在下”的纬线路径
end
end
end
xticks(1:grid_size); yticks(1:grid_size); grid on;
hold off;
% --- 2. 斜纹 (Twill Weave - 2/1 Z-Twill) ---
% 2/1 Z-Twill: 纬线浮过2根经线,再沉过1根经线,织纹向右上方倾斜
figure('Name', 'Twill Weave (2/1 Z-Twill)');
hold on;
axis equal;
axis([0 grid_size 0 grid_size]);
title('斜纹织法 (2/1 Z-Twill)');
xlabel('经线 (Warp - Blue)'); ylabel('纬线 (Weft - Red)');
pattern_repeat = 3; % 2上1下,重复周期为3
float_count = 2; % 浮长为2
% 经线 (蓝色)
for i = 1:grid_size
plot([i i], [0 grid_size], 'b-', 'LineWidth', 1.5);
end
% 纬线 (红色)
for j = 1:grid_size % 每一行纬线
% 绘制完整的纬线背景 (表示其路径,可以稍暗或细)
plot([0.5 grid_size+0.5], [j j], 'Color', [0.8 0.2 0.2], 'LineWidth', 1);
for i = 1:grid_size % 每一个经线交点
% (i-1) gives 0-based index for經線
% (j-1) gives 0-based index for緯線, 用于错位
% mod( (i-1) - (j-1), pattern_repeat) < float_count for S-twill (左上)
% mod( (i-1) + (j-1), pattern_repeat) < float_count for Z-twill (右上)
if mod( (i-1) + (j-1), pattern_repeat) < float_count
% 纬线在经线之上
rectangle('Position', [i-0.25, j-0.25, 0.5, 0.5], ...
'FaceColor', 'r', 'EdgeColor', 'none');
end
end
end
xticks(1:grid_size); yticks(1:grid_size); grid on;
set(gca, 'XTickLabel', [], 'YTickLabel', []); % 隐藏刻度数字
hold off;
% --- 3. 缎纹 (Satin Weave - 5-harness warp satin, step 2) ---
% 5枚经面缎纹,跳2(或3)飞数。这里用跳2为例。
figure('Name', 'Satin Weave (5-harness Warp Satin, Step 2)');
hold on;
axis equal;
axis([0 grid_size 0 grid_size]);
title('缎纹织法 (5枚经面缎纹, 飞数2)');
xlabel('经线 (Warp - Blue)'); ylabel('纬线 (Weft - Red)');
harness = 5; % 5枚
step = 2; % 飞数 (每行纬线的浮点错开的经线数)
% 纬线 (红色) - 缎纹中纬线浮点较少,先画纬线作为背景
for j = 1:grid_size
plot([0.5 grid_size+0.5], [j j], 'r-', 'LineWidth', 1.5);
end
% 经线 (蓝色) - 经面缎纹,经线浮长为主
for i = 1:grid_size % 每一列经线
% 绘制完整的经线背景 (表示其路径)
plot([i i], [0.5 grid_size+0.5], 'Color', [0.2 0.2 0.8], 'LineWidth', 1);
for j = 1:grid_size % 每一个纬线交点
% 计算该行纬线的起始浮点位置
% (i-1) is warp index (0 to N-1)
% (j-1) is weft index (0 to N-1)
% Weft float point for warp i on weft j:
% The first weft float for warp i is at j = mod((i-1)*step, harness)
if mod((j-1) - mod((i-1)*step, harness), harness) == 0
% 此处是纬线浮点 (即经线在此处沉下)
% 我们画的是经面缎纹,所以大部分是经线在上
% 除非是这个特定的交织点,否则经线在上
% rectangle('Position', [i-0.25, j-0.25, 0.5, 0.5], 'FaceColor', 'r', 'EdgeColor', 'none'); % Weft float
else
% 经线在纬线之上
rectangle('Position', [i-0.25, j-0.25, 0.5, 0.5], ...
'FaceColor', 'b', 'EdgeColor', 'none'); % Warp float
end
end
end
xticks(1:grid_size); yticks(1:grid_size); grid on;
set(gca, 'XTickLabel', [], 'YTickLabel', []);
hold off;
% --- 4. 中国结编织 (Chinese Knot - Simplified Grid) ---
figure('Name', 'Chinese Knot');
hold on;
axis equal;
axis([0 grid_size 0 grid_size]);
title('中国结编织 (简化网格)');
xlabel('主线 (Red)'); ylabel('缠绕线 (Blue)');
node_spacing = 2;
radius = 0.7;
theta = linspace(0, 2*pi, 50);
for r = 1:node_spacing:grid_size-1
for c = 1:node_spacing:grid_size-1
% 模拟一个基本的盘长结单元的交叉感
% 红线:主路径
plot(c + radius*cos(theta), r + radius*sin(theta), 'r-', 'LineWidth', 2); % 下层环
plot(c + node_spacing/2 + radius*cos(theta), r + node_spacing/2 + radius*sin(theta), 'r-', 'LineWidth', 2); % 下层环
% 蓝线:缠绕路径 (模拟在上)
% 为了简化,我们用直线段表示交叉
line([c-0.5 c+0.5], [r+0.5 r-0.5], 'Color', 'b', 'LineWidth', 2.5); % 左上到右下
line([c-0.5 c+0.5], [r-0.5 r+0.5], 'Color', 'b', 'LineWidth', 2.5); % 左下到右上
% 更复杂的中国结需要参数曲线或预定义路径
end
end
xticks(1:node_spacing:grid_size); yticks(1:node_spacing:grid_size); grid on;
set(gca, 'XTickLabel', [], 'YTickLabel', []);
hold off;
% --- 5. 六边形竹编 (Hexagonal Bamboo Weave) ---
figure('Name', 'Hexagonal Bamboo Weave');
hold on;
axis equal;
axis([-1 grid_size+1 -1 grid_size+1]); % 调整坐标轴范围以完整显示边缘
title('六边形竹编 (Hexagonal Bamboo Weave)');
xlabel('方向1 (红)'); ylabel('方向2 (绿)'); % z轴方向为蓝色
strip_width = 0.3; % 视觉上的竹条宽度
num_strips_approx = 6; % 大致的竹条数量,会据此计算间距
effective_gs = grid_size + 2; % 扩展绘制区域以覆盖边缘
% 颜色定义
color1 = [0.85, 0.65, 0.35]; % 类似竹子的颜色1 (较浅)
color2 = [0.75, 0.55, 0.25]; % 类似竹子的颜色2 (中等)
color3 = [0.65, 0.45, 0.15]; % 类似竹子的颜色3 (较深)
% 方向1: 水平 (0度)
spacing1 = effective_gs / num_strips_approx;
for i = 0:num_strips_approx
y_center = -1 + i * spacing1;
line([-1, grid_size+1], [y_center, y_center], 'Color', color1, 'LineWidth', strip_width*20); % LineWidth用点数
end
% 方向2: +60度
% y = tan(60)*x + c => c = y - tan(60)*x
% 两条平行线 y=mx+c1, y=mx+c2, 它们沿y轴的截距差是 |c1-c2|
% 它们之间的垂直距离 d = |c1-c2|*cos(alpha) where alpha is angle of line with y-axis
% d = |c1-c2| * cos(30deg) for 60deg lines
% spacing2 = d / cos(30deg)
spacing2_intercept = spacing1 / sind(60); % 沿y轴的截距间距
for i = -num_strips_approx/2 : num_strips_approx % 覆盖整个区域
intercept = i * spacing2_intercept + grid_size/2 - tand(60)*grid_size/2; % 中心化一些
x_vals = [-2, grid_size+2];
y_vals = tand(60) * x_vals + intercept;
line(x_vals, y_vals, 'Color', color2, 'LineWidth', strip_width*20);
end
% 方向3: -60度 (或 120度)
% y = tan(120)*x + c
spacing3_intercept = spacing1 / sind(120); % sind(120) = sind(60)
for i = -num_strips_approx/2 : num_strips_approx
intercept = i * spacing3_intercept + grid_size/2 - tand(120)*grid_size/2;
x_vals = [-2, grid_size+2];
y_vals = tand(120) * x_vals + intercept;
line(x_vals, y_vals, 'Color', color3, 'LineWidth', strip_width*20);
end
xlim([-1 grid_size+1]); ylim([-1 grid_size+1]);
set(gca, 'XTick', [], 'YTick', []); % 隐藏刻度
hold off;
% --- 6. 绕编 (Coiling Weave) ---
figure('Name', 'Coiling Weave');
hold on;
axis equal;
axis([0 grid_size 0 grid_size]);
title('绕编织法 (Coiling Weave)');
xlabel('主螺旋 (Red)'); ylabel('缠绕螺旋 (Blue)');
theta_max = 8*pi; % 螺旋线转更多圈以充满空间
num_points = 500;
theta = linspace(0, theta_max, num_points);
% 主心骨螺旋 (红色)
a_core = 0; % 起始半径
b_core = (grid_size/2 - 1) / theta_max; % 决定螺旋最终大小的参数
r_core = a_core + b_core * theta;
x_core = grid_size/2 + r_core .* cos(theta);
y_core = grid_size/2 + r_core .* sin(theta);
plot(x_core, y_core, 'r-', 'LineWidth', 3);
% 缠绕线 (蓝色),围绕主心骨
offset_angle = pi/2; % 缠绕线相对于主线的相位差
wrap_radius_variation = 0.3 + 0.2*sin(theta*3); % 缠绕半径可以略微变化增加趣味
x_wrap = grid_size/2 + (r_core + wrap_radius_variation) .* cos(theta + offset_angle);
y_wrap = grid_size/2 + (r_core + wrap_radius_variation) .* sin(theta + offset_angle);
plot(x_wrap, y_wrap, 'b-', 'LineWidth', 1.5);
% 第二条缠绕线 (绿色),反向缠绕
x_wrap2 = grid_size/2 + (r_core + wrap_radius_variation*0.8) .* cos(theta - offset_angle - pi/4); % 调整相位和半径
y_wrap2 = grid_size/2 + (r_core + wrap_radius_variation*0.8) .* sin(theta - offset_angle - pi/4);
plot(x_wrap2, y_wrap2, 'g-', 'LineWidth', 1.5);
xticks(0:grid_size); yticks(0:grid_size); grid on;
set(gca, 'XTickLabel', [], 'YTickLabel', []);
hold off;
disp('所有编织可视化已生成。');
代码解释与变更:
斜纹 (Twill Weave):
pattern_repeat = 3 和 float_count = 2 来定义一个 2/1 斜纹(两上一下)。mod( (i-1) + (j-1), pattern_repeat) < float_count 这个条件判断用于确定纬线是否在该交点浮在经线之上。+ (j-1) 的部分(或 - (j-1))用于制造每行纬线的错位,从而形成斜向纹理。我选择了 + 来形成 Z-斜纹(纹路右上方)。rectangle 来填充浮点区域,使其更像织物块。缎纹 (Satin Weave):
harness = 5 (5枚) 和 step = 2 (飞数为2)。if mod((j-1) - mod((i-1)*step, harness), harness) == 0 判断的是纬线浮点的位置。对于经面缎纹,我们主要画经线浮起,所以 else 条件下画蓝色(经线)方块。中国结编织 (Chinese Knot):
六边形竹编 (Hexagonal Bamboo Weave):
strip_width 定义了竹条的视觉宽度 (通过 LineWidth 实现)。spacing1, spacing2_intercept, spacing3_intercept 用于计算各方向竹条的间距,使其能均匀分布并形成六边形孔眼。num_strips_approx 可以改变竹条的密度。LineWidth 设置得比较大。绕编 (Coiling Weave):
wrap_radius_variation 使缠绕半径略有变化,增加一点自然感。现在,运行这段Matlab代码,你应该能看到修正后的斜纹和新的六边形竹编的可视化效果,以及其他几种编织的几何构型。
请给出平纹,斜纹,缎纹,中国结编织和竹编(六边形)还有绕编的几何构型(限制五个单元内)可视化代码(matlab),其中,为了更加直观,经纬线用条带表示,并且这些编织方式均留有一定的孔隙
好的,这次我们使用 rectangle 来表示条带,并通过调整条带的宽度和位置来留出孔隙。我们将所有编织限制在 5x5 的单元格内。
核心思路:
strip_width = 0.7),这样条带之间就会有 1 - strip_width 的孔隙。rectangle 的 Position 和 FaceColor 来实现。% --- 清理工作区和图形 ---
close all; clear; clc;
grid_dim = 5; % 网格维度 (5x5单元)
strip_width = 0.7; % 条带宽度 (小于1以留出孔隙)
pore_size = 1 - strip_width; % 孔隙大小
offset = pore_size / 2; % 条带从单元格边缘的偏移
% 颜色定义
warp_color = [0.2, 0.5, 0.8]; % 经线颜色 (蓝色系)
weft_color = [0.9, 0.3, 0.3]; % 纬线颜色 (红色系)
knot_color1 = [0.8, 0.2, 0.2]; % 中国结主色
knot_color2 = [0.2, 0.2, 0.7]; % 中国结辅色
bamboo_color1 = [0.85, 0.65, 0.35]; % 竹色1
bamboo_color2 = [0.75, 0.55, 0.25]; % 竹色2
bamboo_color3 = [0.65, 0.45, 0.15]; % 竹色3
coil_core_color = [0.8, 0.4, 0.2]; % 绕编核心色
coil_wrap_color = [0.3, 0.7, 0.4]; % 绕编缠绕色
% --- Helper function to draw a strip segment in a cell ---
% (Not strictly necessary, but can make main code cleaner if reused heavily)
% draw_strip = @(x_cell, y_cell, orientation, color) ...
% orientation == 'v' ? ... % Vertical (warp)
% rectangle('Position', [x_cell-0.5+offset, y_cell-0.5, strip_width, 1], ...
% 'FaceColor', color, 'EdgeColor', 'none') : ...
% orientation == 'h' ? ... % Horizontal (weft)
% rectangle('Position', [x_cell-0.5, y_cell-0.5+offset, 1, strip_width], ...
% 'FaceColor', color, 'EdgeColor', 'none') : ...
% [];
% --- 1. 平纹 (Plain Weave) ---
figure('Name', 'Plain Weave (Strips & Pores)');
hold on;
axis equal;
axis([0 grid_dim 0 grid_dim]);
title('平纹 (5x5)');
set(gca, 'XTick', [], 'YTick', []);
% 先画所有经线 (垂直条带)
for i = 1:grid_dim % 经线索引
rectangle('Position', [i-0.5+offset, 0, strip_width, grid_dim], ...
'FaceColor', warp_color, 'EdgeColor', 'none');
end
% 再画纬线 (水平条带),根据平纹规则在经线上或下
for j = 1:grid_dim % 纬线索引
for i = 1:grid_dim % 经线相交处
if mod(i+j, 2) == 0 % 纬线在经线之上
rectangle('Position', [i-0.5, j-0.5+offset, 1, strip_width], ...
'FaceColor', weft_color, 'EdgeColor', 'none');
else
% 纬线在经线之下 (此部分已被经线覆盖,但为了完整性可以画短节)
% 如果要画,需要把经线也切分成段
% 简单处理:不画此段,经线自然显露
end
end
end
hold off;
% --- 2. 斜纹 (Twill Weave - 2/1 Z-Twill) ---
figure('Name', 'Twill Weave (Strips & Pores)');
hold on;
axis equal;
axis([0 grid_dim 0 grid_dim]);
title('斜纹 2/1 Z (5x5)');
set(gca, 'XTick', [], 'YTick', []);
pattern_repeat_twill = 3;
float_count_twill = 2;
% 先画所有经线 (垂直条带)
for i = 1:grid_dim
rectangle('Position', [i-0.5+offset, 0, strip_width, grid_dim], ...
'FaceColor', warp_color, 'EdgeColor', 'none');
end
% 再画纬线 (水平条带),根据斜纹规则
for j = 1:grid_dim % 纬线行
for i = 1:grid_dim % 与经线交点
% (i-1) for 0-based warp index, (j-1) for 0-based weft index
if mod( (i-1) + (j-1), pattern_repeat_twill) < float_count_twill
% 纬线浮在经线之上
rectangle('Position', [i-0.5, j-0.5+offset, 1, strip_width], ...
'FaceColor', weft_color, 'EdgeColor', 'none');
end
% 否则纬线在经线之下,经线已画
end
end
hold off;
% --- 3. 缎纹 (Satin Weave - 5-harness Warp Satin, Step 2, but harness=3 for 5x5 demo) ---
figure('Name', 'Satin Weave (Strips & Pores)');
hold on;
axis equal;
axis([0 grid_dim 0 grid_dim]);
title('缎纹 (经面, 3枚飞数1, 5x5)'); % 5枚在5x5里不明显,用3枚1飞演示
set(gca, 'XTick', [], 'YTick', []);
harness_satin = 3; % 用3枚演示,因为5x5太小
step_satin = 1; % 飞数
% 先画所有纬线 (水平条带),因为是经面缎,纬线大多在下
for j = 1:grid_dim
rectangle('Position', [0, j-0.5+offset, grid_dim, strip_width], ...
'FaceColor', weft_color, 'EdgeColor', 'none');
end
% 再画经线 (垂直条带),根据缎纹规则
for i = 1:grid_dim % 经线列
for j = 1:grid_dim % 与纬线交点
% (i-1) for warp index, (j-1) for weft index
% 经面缎:判断哪里是纬线浮点,其他地方都是经线浮点
is_weft_float_point = (mod((j-1) - mod((i-1)*step_satin, harness_satin), harness_satin) == 0);
if ~is_weft_float_point
% 经线浮在纬线之上
rectangle('Position', [i-0.5+offset, j-0.5, strip_width, 1], ...
'FaceColor', warp_color, 'EdgeColor', 'none');
end
% 否则是纬线浮点,纬线已画
end
end
hold off;
% --- 4. 中国结编织 (Simplified Grid) ---
figure('Name', 'Chinese Knot (Strips & Pores)');
hold on;
axis equal;
axis([0 grid_dim 0 grid_dim]);
title('中国结 (简化, 5x5)');
set(gca, 'XTick', [], 'YTick', []);
knot_strip_w = 0.4; % 中国结条带可以细一点
knot_offset = (1 - knot_strip_w)/2;
for r_idx = 1:grid_dim
for c_idx = 1:grid_dim
center_x = c_idx - 0.5;
center_y = r_idx - 0.5;
% 模拟单元内的交叉
% "下层" 条带 (先画)
rectangle('Position', [center_x - knot_strip_w/2, center_y - 0.4, knot_strip_w, 0.8], ...
'FaceColor', knot_color1, 'EdgeColor', 'none', 'Curvature', [0.3 0.3]);
rectangle('Position', [center_x - 0.4, center_y - knot_strip_w/2, 0.8, knot_strip_w], ...
'FaceColor', knot_color1, 'EdgeColor', 'none', 'Curvature', [0.3 0.3]);
% "上层" 条带 (后画,略微偏移以示层次)
if mod(r_idx+c_idx,2) == 0
rectangle('Position', [center_x - knot_strip_w*0.7/2, center_y - 0.35, knot_strip_w*0.7, 0.7], ...
'FaceColor', knot_color2, 'EdgeColor', 'k', 'LineWidth',0.1, 'Curvature', [0.5 0.5]);
else
rectangle('Position', [center_x - 0.35, center_y - knot_strip_w*0.7/2, 0.7, knot_strip_w*0.7], ...
'FaceColor', knot_color2, 'EdgeColor', 'k', 'LineWidth',0.1, 'Curvature', [0.5 0.5]);
end
end
end
hold off;
% --- 5. 六边形竹编 (Hexagonal Bamboo Weave) ---
figure('Name', 'Hexagonal Bamboo Weave (Strips & Pores)');
hold on;
axis equal;
axis([-1 grid_dim+1 -1 grid_dim+1]);
title('六边形竹编 (5x5 approx)');
set(gca, 'XTick', [], 'YTick', []);
hex_strip_width = 0.5; % 竹条宽度
num_strips_approx_hex = 4; % 大致竹条数,会调整
center_x_hex = grid_dim/2;
center_y_hex = grid_dim/2;
% 定义绘制旋转矩形的函数
draw_rotated_strip = @(x, y, w, l, angle_deg, color) ...
plot(polyshape(rotate(rectangle('Position',[-l/2, -w/2, l, w],'Curvature',0.05), angle_deg,[0,0]) + [x,y]), ...
'FaceColor', color, 'EdgeColor', 'none');
% 方向1: 0度 (水平)
spacing_hex = (grid_dim+1) / num_strips_approx_hex;
for i_hex = 0:num_strips_approx_hex
y_center_hex = -0.5 + i_hex * spacing_hex;
draw_rotated_strip(center_x_hex, y_center_hex, hex_strip_width, grid_dim+2, 0, bamboo_color1);
end
% 方向2: 60度
for i_hex = -num_strips_approx_hex/1.5 : num_strips_approx_hex/1.5
% 计算偏移,使得条带大致通过中心区域
offset_val = i_hex * spacing_hex * 1.15; % 调整系数以获得好的间距
draw_rotated_strip(center_x_hex + offset_val*cosd(30), ...
center_y_hex + offset_val*sind(30), ...
hex_strip_width, grid_dim+2, 60, bamboo_color2);
end
% 方向3: 120度
for i_hex = -num_strips_approx_hex/1.5 : num_strips_approx_hex/1.5
offset_val = i_hex * spacing_hex * 1.15;
draw_rotated_strip(center_x_hex + offset_val*cosd(-30), ...
center_y_hex + offset_val*sind(-30), ...
hex_strip_width, grid_dim+2, 120, bamboo_color3);
end
xlim([-1 grid_dim+1]); ylim([-1 grid_dim+1]);
hold off;
% --- 6. 绕编 (Coiling Weave) ---
figure('Name', 'Coiling Weave (Strips & Pores)');
hold on;
axis equal;
axis([0 grid_dim 0 grid_dim]);
title('绕编 (5x5)');
set(gca, 'XTick', [], 'YTick', []);
theta_max_coil = 5*pi; % 圈数调整以适应5x5
num_points_coil = 200;
theta_coil = linspace(0, theta_max_coil, num_points_coil);
% 主心骨螺旋
a_core_coil = 0.1; % 起始半径
b_core_coil = (grid_dim/2 - 0.5) / theta_max_coil;
r_core_coil = a_core_coil + b_core_coil * theta_coil;
x_core_coil = grid_dim/2 + r_core_coil .* cos(theta_coil);
y_core_coil = grid_dim/2 + r_core_coil .* sin(theta_coil);
% 缠绕线
offset_angle_coil = pi/3; % 缠绕线的相位差
wrap_dist_coil = 0.25; % 缠绕线离主心骨的距离 (条带半径)
coil_strip_w = 0.3; % 绕编条带宽度
% 使用patch绘制螺旋条带
% Helper to create strip vertices from a centerline
create_strip_poly = @(x_center, y_center, width_val) ...
[ [x_center(1:end-1) - width_val/2 .* diff(y_center)./sqrt(diff(x_center).^2+diff(y_center).^2), ... % P1x
flip(x_center(1:end-1) + width_val/2 .* diff(y_center)./sqrt(diff(x_center).^2+diff(y_center).^2)) ]; ... % P2x (flipped)
[y_center(1:end-1) + width_val/2 .* diff(x_center)./sqrt(diff(x_center).^2+diff(y_center).^2), ... % P1y
flip(y_center(1:end-1) - width_val/2 .* diff(x_center)./sqrt(diff(x_center).^2+diff(y_center).^2)) ] ]; % P2y (flipped)
% 绘制主螺旋条带
if numel(x_core_coil)>1
dx_core = diff(x_core_coil); dy_core = diff(y_core_coil);
len_core = sqrt(dx_core.^2 + dy_core.^2);
nx_core = -dy_core./len_core; ny_core = dx_core./len_core; % Normals
x_poly_core = [x_core_coil(1:end-1) + nx_core*coil_strip_w/2, ...
fliplr(x_core_coil(1:end-1) - nx_core*coil_strip_w/2)];
y_poly_core = [y_core_coil(1:end-1) + ny_core*coil_strip_w/2, ...
fliplr(y_core_coil(1:end-1) - ny_core*coil_strip_w/2)];
patch(x_poly_core, y_poly_core, coil_core_color, 'EdgeColor', 'none');
end
% 绘制缠绕螺旋条带
x_wrap_center = grid_dim/2 + (r_core_coil + wrap_dist_coil) .* cos(theta_coil + offset_angle_coil);
y_wrap_center = grid_dim/2 + (r_core_coil + wrap_dist_coil) .* sin(theta_coil + offset_angle_coil);
if numel(x_wrap_center)>1
dx_wrap = diff(x_wrap_center); dy_wrap = diff(y_wrap_center);
len_wrap = sqrt(dx_wrap.^2 + dy_wrap.^2);
nx_wrap = -dy_wrap./len_wrap; ny_wrap = dx_wrap./len_wrap;
x_poly_wrap = [x_wrap_center(1:end-1) + nx_wrap*coil_strip_w/2, ...
fliplr(x_wrap_center(1:end-1) - nx_wrap*coil_strip_w/2)];
y_poly_wrap = [y_wrap_center(1:end-1) + ny_wrap*coil_strip_w/2, ...
fliplr(y_wrap_center(1:end-1) - ny_wrap*coil_strip_w/2)];
patch(x_poly_wrap, y_poly_wrap, coil_wrap_color, 'EdgeColor', 'none');
end
hold off;
disp('所有5x5条带编织可视化已生成。');
代码解释和关键点:
grid_dim = 5: 限制在5x5单元。
strip_width = 0.7: 条带宽度为0.7个单元,则孔隙为0.3个单元。
offset = pore_size / 2: 用于将条带在其“通道”内居中。
平纹、斜纹、缎纹:
rectangle 绘制的是条带而不是线。Position 参数的 x, y 是矩形左下角,width, height 是宽高。strip_width,高度是整个单元格(1)或整个网格 (grid_dim)。strip_width,宽度是整个单元格(1)或整个网格。中国结编织:
'Curvature', [0.3 0.3] 给矩形添加圆角。六边形竹编:
draw_rotated_strip 辅助函数: 用于绘制旋转的矩形条带。它创建一个基础矩形,旋转,然后平移到目标位置,并使用 polyshape 和 plot 进行填充绘制。0, 60, 120 度)。spacing_hex, num_strips_approx_hex) 需要调整以在5x5区域内形成可见的六边形孔隙。绕编:
patch 函数: 用于绘制螺旋形的条带。coil_strip_w/2 的距离,得到条带的两个边缘点。patch) 并填充颜色。注意事项:
patch,这比简单画粗线更接近“条带”的概念。运行此代码将生成6个图形窗口,分别展示这几种编织方式在5x5单元格内使用条带和孔隙的可视化效果。