線段裁剪代碼-MATLAB_第1頁
線段裁剪代碼-MATLAB_第2頁
線段裁剪代碼-MATLAB_第3頁
線段裁剪代碼-MATLAB_第4頁
線段裁剪代碼-MATLAB_第5頁
已閱讀5頁,還剩4頁未讀, 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進(jìn)行舉報或認(rèn)領(lǐng)

文檔簡介

1、四、線段裁剪%Sutherland_Test.m 文件,主文件函數(shù) clear all;clc;MainMenu();%check_callback.m 文件,查看圖形函數(shù)function check_callback()%查看原始線段圖形回調(diào)函數(shù)%lines 矩陣共四列,每一列的含義如下:%端點(diǎn) A x 坐標(biāo) 端點(diǎn) A y 坐標(biāo) 端點(diǎn) B x 坐標(biāo) %lines=getappdata(0,'lines');if isempty(lines)errordlg(' 當(dāng)前尚未生成線段,無法顯示! '); return;endfigure();hold on;for

2、i=1:length(lines(:,1)plot(lines(i,1,3),lines(i,2,4);endhold off;end端點(diǎn) B y 坐標(biāo)%Cohen_Sutherland.m 文件,編碼裁剪算法function Lines=Cohen_Sutherland(line,Rectangle)%編碼裁剪算法%line 為線段端點(diǎn)矩陣 ,共四列,其數(shù)據(jù)含義如下:% 端點(diǎn) A x 坐標(biāo) 端點(diǎn) A y 坐標(biāo) 端點(diǎn) B x 坐標(biāo) %Rectangle 為窗口邊界值,共四個元素,其含義分別為 %首先檢測參數(shù)是否合法row column=size(line);if column<4|len

3、gth(Rectangle)<4Lines=;fprintf(' 參數(shù)不合法不合法 ');return ;end%程序中主要變量說明%code 為線段端點(diǎn)的編碼矩陣,兩行四列,第一行為點(diǎn)% 四列的含義為: D0,D1,D2,D3端點(diǎn) B y 坐標(biāo)Xwl,Xwr,Ywb,Ywt 。P1 的編碼,第二行為點(diǎn) P2 的編碼%依次處理 line 的各個線段k=0;Lines=;for i=1:length(line(:,1)%取出第 i 條線段P1=line(i,1,2);P2=line(i,3,4);%計算斜率PP=P1-P2;if PP(1)=0k=inf;elsek=PP(

4、2)/PP(1);endfinished=false;while(finished)%對點(diǎn) P1 和 P2 進(jìn)行編碼code=P1(1)<Rectangle(1),P1(1)>Rectangle(2),P1(2)<Rectangle(3),P1(2)>Rectangle(4);P2(1)<Rectangle(1),P2(1)>Rectangle(2),P2(2)<Rectangle(3),P2(2)>Rectangle(4); ;% P1,P2,k,code%進(jìn)行簡取或簡棄的判斷 test=code(1,:)|code(2,:); %判斷是否簡取

5、 if isempty(find(test>0,1)Lines=Lines;P1,P2;finished=true;end%若當(dāng)前線段處理完成,則退出if finishedbreak;end%判斷是否簡棄test=code(1,:)&code(2,:);if isempty(find(test>0, 1)finished=true;endif finishedbreak;end%確保 P1 在窗口之外if isempty(find(code(1,:)>0,1)%交換P1,P2的坐標(biāo)值和編碼PT=P1;P1=P2;P2=PT;PT=code(1,:);code(1,:)

6、=code(2,:);code(2,:)=PT;end %從低位開始找編碼值為 1 的地方 D=find(code(1,:)>0,1);if D<=2%此時 P1 位于窗口的左邊或右邊if k=0 %若是水平線,則 y 不變, x 變?yōu)榇翱诘淖筮吔缁蛴疫吔?%且此時 k 不會等于 inf ,否則線段處于簡棄狀態(tài)。 P1(1)=Rectangle(D); %P1(2)=Rectangle(find(code(1,3,4)>0,1);else%若線段是斜線,則計算y值,x值變?yōu)榇翱诘淖筮吔缁蛴疫吔鏟1=Rectangle(D),P1(2)+k*(Rectangle(D)-P1(1

7、);endelse%此時 P1 位于窗口的上方或下方if k=inf%若線段是豎直線,則X不變,y變?yōu)榇翱诘纳线吔缁蛳逻吔?%且此時 k 不會等于 0,否則線段將處于簡棄狀態(tài)。P1(2)=Rectangle(D);else%若線段是斜線,則計算X值,y值變?yōu)榇翱诘纳线吔缁蛳逻吔?。P1=P1(1)+(Rectangle(D)-P1(2)/k,Rectangle(D);endend%對P1和P2重新編碼,再次計算。P1,P2,k,codepause(10);% scanf(D,'%d');% inputdlg(''); endend %對最終點(diǎn)進(jìn)行取整運(yùn)算 %Lin

8、es=round(Lines);end %DrawOriginalGraph.m 文件,繪制原始線段圖形函數(shù) function DrawOriginalGraph()%繪制原始線段圖形函數(shù)%lines 矩陣共四列,每一列的含義如下:% 端點(diǎn) A x 坐標(biāo)端點(diǎn) A y 坐標(biāo) 端點(diǎn) B x 坐標(biāo) 端點(diǎn) B y 坐標(biāo) % lines=getappdata(0,'lines');if isempty(lines)errordlg(' 當(dāng)前尚未生成線段,無法顯示! '); return;endfigure('name',' 原始線段圖形 '

9、);hold on;for i=1:length(lines(:,1) plot(lines(i,1,3),lines(i,2,4);endhold off;end %DrawSutherlandGraph.m 文件,繪制編碼裁剪算法裁剪后線段函數(shù) function DrawSutherlandGraph(OriginalLines,Rectangle,SutherlandedLines) %繪制裁剪前后線段對比圖%OriginalLines 是初始線段矩陣 ,Rectangle 是裁剪窗口參數(shù), SutherlandedLines 是裁剪后的線 段矩陣%OriginalLines 與 Sut

10、herlandedLines 分別有四列,每一列的參數(shù)定義相同,如下所示: %端點(diǎn) A x 坐標(biāo) 端點(diǎn) A y 坐標(biāo) 端點(diǎn) B x 坐標(biāo) 端點(diǎn) B y 坐標(biāo) %Rectangle 共四個元素,每個元素的定義分別為 Xwl,Xwr,Ywb,Ywt 。figure('name',' 線段原始圖形與裁剪后的對比圖 '); %在第一個小圖里繪制原始線段圖形以及裁剪窗口 subplot(2,1,1);hold on;%繪制原始線段for i=1:length(OriginalLines(:,1) plot(OriginalLines(i,1,3),OriginalLine

11、s(i,2,4);end%繪制裁剪窗口 R=Rectangle;RLines=R(1),R(3),R(2),R(3);R(2),R(3),R(2),R(4);R(2),R(4),R(1),R(4);R(1),R(4),R(1),R(3); ;for i=1:length(RLines(:,1) plot(RLines(i,1,3),RLines(i,2,4),'-r');end hold off;%在第二個小圖里繪制裁剪后的圖形及裁剪窗口 subplot(2,1,2);hold on;for i=1:length(SutherlandedLines(:,1)plot(Suthe

12、rlandedLines(i,1,3),SutherlandedLines(i,2,4);end%繪制裁剪窗口for i=1:length(RLines(:,1)plot(RLines(i,1,3),RLines(i,2,4),'-r');end hold off; end %generate_callback.m 文件,function generate_callback()%生成線段回調(diào)函數(shù)%prompt=' 輸入線段條數(shù) ','線段端點(diǎn) x 坐標(biāo)最大值 ','線段端點(diǎn) y 坐標(biāo)最大值 '% 設(shè)置提示字符 串title=

13、9; 生成線段 '%設(shè)置標(biāo)題 numline=1;% 指定輸入數(shù)據(jù)行數(shù) defdata='20','100','100'% 指定數(shù)據(jù)的默認(rèn)值Resize='o n'%設(shè)置對話框大小為可調(diào)節(jié)的answer=inputdlg(prompt,title,numline,defdata,Resize); %若用戶點(diǎn)擊“取消”鍵,則直接退出if isempty(answer)return;end%將輸入對話框的輸入值轉(zhuǎn)化為浮點(diǎn)數(shù) data=str2num(char(answer);lines=GenerateLines(round(

14、data(1),data(2),data(3); setappdata(0,'lines',lines);answer=questdlg('是否查看已生成的線段','提問對話框,是','否','是');%an swer,strcm p(an swer,'是')if strcm p(an swer,'是')%繪制原始線段圖形DrawOriginalGraph();end end %GenereateLines.m 文件,隨機(jī)生成線段函數(shù) function Lines=GenerateL

15、ines(count,MaxX,MaxY) %隨機(jī)生成 count 條線段,以矩陣 Lines 返回。%MaxX 是線段端點(diǎn) x 坐標(biāo)的最大值, MaxY 是線段端點(diǎn) y 坐標(biāo)最大值%Lines 矩陣共四列,每一列的含義如下:%端點(diǎn) A x 坐標(biāo) 端點(diǎn) A y 坐標(biāo) %line=rand(count,4); line(:,1,3)=rand(count,2)*MaxX; line(:,2,4)=rand(count,2)*MaxY;Lines=line;end端點(diǎn) B x 坐標(biāo) 端點(diǎn) B y 坐標(biāo)%MainMenu.m 文件,這界面函數(shù)function MainMenu() %程序主菜單界面

16、% leftbase=0; bottombase=-50; %初始化線段矩陣 lines setappdata(0,'lines',); figure(); uicontrol('Style','pushbutton','string',' 50,.'callback','generate_callback'); uicontrol('Style','pushbutton','string',''callback',&#

17、39;sutherland_callback');生 成 線 段 ','position',200+leftbase 350+bottombase 100裁剪 ','position',200+leftbase 280+bottombase 100 50,.uicontrol('Style','pushbutton','string',' 查看原始線段圖形 ','position',200+leftbase 210+bottombase 100 50,.

18、9;callback','check_callback');end%sutherland_callback.m 文件function sutherland_callback() %裁剪算法回調(diào)函數(shù)% lines=getappdata(0,'lines');if isempty(lines)errordIgC當(dāng)前尚未生成線段!n請先生成線段','錯誤提示');return;endwhile trueprompt=' 窗口參數(shù) Xwl',' 窗口參數(shù) Xwr',' 窗口參數(shù) Ywb',' 窗口參數(shù) Ywt'% 設(shè)置提示字符 串title=' 窗口參數(shù) '%設(shè)置標(biāo)題

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論