这是一个很好的方法,将一条线与矩形网格相交并得到每个交叉段的长度:我使用了来自这个

link的第三个答案中的伪代码的线条交点

% create some line form the equation y=mx+h

m = 0.5; h = 0.2;

x = -2:0.01:2;

y = m*x+h;

% create a grid on the range [-1,1]

[X,Y] = meshgrid(linspace(-1,1,10),linspace(-1,1,10));

% create a quad mesh on this range

fvc = surf2patch(X,Y,zeros(size(X)));

% extract topology

v = fvc.vertices(:,[1,2]);

f = fvc.faces;

% plot the grid and the line

patch(fvc,'EdgeColor','g','FaceColor','w'); hold on;

plot(x,y);

% use line line intersection from the link

DC = [f(:,[1,2]);f(:,[2,3]);f(:,[3,4]);f(:,[4,1])];

D = v(DC(:,1),:);

C = v(DC(:,2),:);

A = repmat([x(1),y(1)],size(DC,1),1);

B = repmat([x(end),y(end)],size(DC,1),1);

E = A-B;

F = D-C;

P = [-E(:,2),E(:,1)];

h = dot(A-C,P,2)./dot(F,P,2);

% calc intersections

idx = (0<=h & h<=1);

intersections = C(idx,:)+F(idx,:).*repmat(h(idx),1,2);

intersections = uniquetol(intersections,1e-8,'ByRows',true);

% sort by x axis values

[~,ii] = sort(intersections(:,1));

intersections = intersections(ii,:);

scatter(intersections(:,1),intersections(:,2));

% get segments lengths

directions = diff(intersections);

lengths = sqrt(sum(directions.^2,2));

directions = directions./repmat(sqrt(sum(directions.^2,2)),1,2);

directions = directions.*repmat(lengths,1,2);

quiver(intersections(1:end-1,1),intersections(1:end-1,2),directions(:,1),directions(:,2),'AutoScale','off','Color','k');

这是结果(图像中箭头的长度是段长度)

matlab 判断矩形相交,如何在matlab中获取线矩形交叉段相关推荐

  1. java圆角矩形_如何在java中使用普通矩形轮廓绘制圆角矩形

    我可以想到两种方法.第一种是生成一个表示方形外边缘和圆形内边缘的Shape. 第二种方法是使用AlphaComposite生成掩码结果. public class TestMask { public ...

  2. java+script+当前日期_如何在JavaScript中获取当前日期?

    如何在JavaScript中获取当前日期? #1楼 您可以使用扩展了 Date对象的Date.js库,从而可以使用.today()方法. #2楼 如果您想对日期格式进行更多的粒度控制,我强烈建议您查看 ...

  3. 如何在PHP中获取客户端IP地址[重复]

    本文翻译自:How to get the client IP address in PHP [duplicate] This question already has an answer here: ...

  4. 如何在JavaScript中获取时间戳

    如何在JavaScript中获取时间戳 +运算符 我们可以使用+运算符将日期对象直接转换为UNIX时间戳. 例如,我们可以这样写: +new Date() +日期对象之前操作者触发valueOf的方法 ...

  5. 关于Activity的getReferrer():如何在Activity中获取调用者?

    http://blog.csdn.net/u013553529/article/details/53856800 关于Activity的getReferrer()之一:如何在Activity中获取调用 ...

  6. java如何实取随机数_java - 如何在Kotlin中获取随机数?

    java - 如何在Kotlin中获取随机数? 可以在2个参数之间返回随机整数的通用方法,如ruby,可以使用rand(0..n). 有什么建议吗? 16个解决方案 208 votes 我的建议是In ...

  7. php mysql 到表最后_如何在PHP中获取MySQL表的最后插入ID?

    如何在PHP中获取MySQL表的最后插入ID? 我有一张表,经常插入新数据. 我需要获取表格的最后一个ID. 我怎样才能做到这一点? 它类似于SELECT MAX(id) FROM table吗? g ...

  8. 如何在JavaScript中获取字符串数组的字符串?

    本文翻译自:How do you get a string to a character array in JavaScript? How do you get a string to a chara ...

  9. java文件中获取创建日期_如何在Java中获取文件的上次修改日期

    java文件中获取创建日期 Sometimes we need to get the file last modified date in Java, usually for listeners li ...

  10. jquery 获取同级元素_如何在jQuery中获取元素的同级

    jquery 获取同级元素 In this post, we will discuss how to get the siblings of an HTML element in jQuery. jQ ...

最新文章

  1. 2019 ICPC Asia Nanjing Regional K.Triangle(求一个能将三角形分成两个面积相同的线段、计算几何)
  2. file_get_contents遍历api数据
  3. 机器学习实战(十)利用K-means算法对未标注数据分组
  4. javascript中动态添加事件!!
  5. php隐藏webshell_PHP 安全的十个必备技巧
  6. php连接excel表格数据类型,TP5 中使用PHPExcel 导出导入数据到excel表格
  7. [Java] 蓝桥杯ADV-184 算法提高 素数求和
  8. 读取文件内容返回ListString类型
  9. FFTW3在VS环境下的安装(亲测)
  10. linux万兆网卡驱动下载,Intel英特尔PCIe万兆网卡驱动5.11.3版For Linux(2021年3月5日发布)...
  11. easyexcel 第一次导入导出会报错com.alibaba.excel.exception.ExcelAnalysisException,所以自定义excel导入导出表格
  12. Base64方式上传文件
  13. 线性方程组个数和方程未知数与方程解的关系
  14. IE下载附件,文件大小超过10M后 无法下载
  15. 【Unity】Unity 欧拉角、四元数、万向节死锁、四元数转轴角
  16. windows Git下载与安装
  17. 前后端分离状态保持问题之JWT
  18. sql服务器查看版本信息,SQL Server 各种版本号的查看
  19. Multi-Objective Data Placement for Multi-Cloud Socially Aware Services---INFOCOM 2014
  20. IOS 通过麦克风检测声音分贝

热门文章

  1. Windows下安装PyQt4+python2.7+(nltk+wordcloud+jieba+pyinstaller打包)——词频分析软件
  2. 数学中的全微分(方程),全导数(公式),偏微分(方程),梯度,斜率,导数,方向导数等
  3. macbook air 安装双系统windows10手册及避坑技巧
  4. 智能建造如何转型升级?智慧工地引领建筑行业智能化信息化发展
  5. 5g的八大关键指标_ITU为5G确定了八大能力指标
  6. 支付宝“美团”化 美团“支付宝”化
  7. Android开发之中文语音朗读
  8. 以色列宣布启动“创世纪2”号登月计划;我国粮食生产实现“十七连丰”丨科技新闻...
  9. URL 长链地址转短链 URL地址
  10. 手把手教你注册和备案域名