• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            Tauruser

            Enjoy Every Day
            posts - 34, comments - 95, trackbacks - 0, articles - 5
              C++博客 :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理

            05年寫的直線裁剪算法

            Posted on 2007-01-28 10:28 Tauruser 閱讀(2773) 評論(2)  編輯 收藏 引用 所屬分類: 計算機圖形學
            頭文件<line.h>

            #pragma?once
            #include?
            "afx.h"
            #include?
            "atltypes.h"
            /*
            ?*?Powered?by?Tauruser
            ?*?Tauruser~gmail.com
            ?*?Written?on?2005.12
            ?*?歡迎轉載,請說明出處
            ?
            */
            ?
            class?CLine?:
            ????
            public?CObject
            {
            ????DECLARE_SERIAL?(CLine)
            public:
            ????CLine(
            void);
            ????
            ~CLine(void);
            private:
            ????CPoint?m_ptFrom;
            private:
            ????CPoint?m_ptTo;
            public:
            ????CLine(CPoint?ptFrom,?CPoint?ptTo);
            ????
            void?Draw(CDC*?pDC);
            ????
            void?Serialize(CArchive&?ar);
            ????CLine
            *?SetLine(CPoint?ptFrom,?CPoint?ptTo);
            ????
            //?剪裁線段,參數為窗口左上角和右下角坐標,當線段在窗口內返回TRUE,否則

            FALSE
            ????
            bool?Cut(CPoint?ptUpLeft,?CPoint?ptDownRight);
            }
            ;

            源碼<line.cpp>

            #include?"StdAfx.h"
            #include?
            ".\line.h"
            /*
            ?*?Powered?by?Tauruser
            ?*?Tauruser~gmail.com
            ?*?Written?on?2005.12
            ?*?歡迎轉載,請說明出處
            ?
            */
            ?
            IMPLEMENT_SERIAL?(CLine,CObject,
            1)
            CLine::CLine(
            void)
            :?m_ptFrom(
            0)
            ,?m_ptTo(
            0)
            {
            }


            CLine::
            ~CLine(void)
            {
            }


            CLine::CLine(CPoint?ptFrom,?CPoint?ptTo)
            {
            ????m_ptFrom
            =ptFrom;
            ????m_ptTo
            =ptTo;
            }


            void?CLine::Draw(CDC*?pDC)
            {
            ????pDC
            ->MoveTo(m_ptFrom);
            ????pDC
            ->LineTo(m_ptTo);
            }


            void?CLine::Serialize(CArchive&?ar)
            {
            ????CObject::Serialize(ar);
            ????
            if(ar.IsStoring())
            ????????ar
            <<m_ptFrom<<m_ptTo;
            ????
            else
            ????????ar
            >>m_ptFrom>>m_ptTo;
            }


            CLine
            *?CLine::SetLine(CPoint?ptFrom,?CPoint?ptTo)
            {
            ????m_ptFrom
            =ptFrom;
            ????m_ptTo
            =ptTo;
            ????
            return?this;
            }


            //?剪裁線段,參數為窗口左上角和右下角坐標,當線段在窗口內返回TRUE,否則FALSE
            bool?CLine::Cut(CPoint?ptUpLeft,?CPoint?ptDownRight)
            {
            ????
            //long?iA,iB,iC,iD,iXl,iXr,iYt,iYb;?
            ????const?long?iA(m_ptFrom.x),?iB(m_ptFrom.y),?iC(m_ptTo.x),?iD(m_ptTo.y);
            ????
            long?iXl=ptUpLeft.x,?iXr=ptDownRight.x,?iYt=ptUpLeft.y,?iYb=ptDownRight.y;
            ????
            int?temp;
            ????
            if?(iXl>iXr)?
            ????
            {
            ????????temp
            =iXl;
            ????????iXl
            =iXr;
            ????????iXr
            =temp;
            ????}

            ????
            if(iYb>iYt)
            ????
            {
            ????????temp
            =iYb;
            ????????iYb
            =iYt;
            ????????iYt
            =temp;
            ????}


            ????
            if(iXl<=iA?&&?iA<=iXr)
            ????
            {
            ????????
            if(iYb<=iB?&&?iB<=iYt)
            ????????
            {????;//m_ptFrom保持原值
            ????????}
            else?if(iB<iYb?&&?iD<iYb)?
            ????????????
            return?false;//與窗口無交.
            ????????else?if?(iB<iYb?&&?iD>=iYb)
            ????????
            {
            ????????????
            //1.2
            ????????????int?x;
            ????????????x
            =iA-((iB-iYb)*(iA-iC)/(iB-iD));
            ????????????
            if(iXl<=x?&&?x<=iXr)//檢驗x的有校性
            ????????????{
            ????????????????m_ptFrom.x
            =x;
            ????????????????m_ptFrom.y
            =iYb;

            ????????????}
            else?return?false;//否則無交點

            ????????}
            else?if(iB>iYt?&&?iD>iYt)?????return?false;//與窗口無交
            ????????else?if(iB>iYt?&&?iD<=iYt)
            ????????
            {????
            ????????????
            //1.3
            ????????????int?x;
            ????????????x
            =iA-((iB-iYt)*(iA-iC)/(iB-iD));//檢驗x的有校性?
            ????????????if(iXl<=x?&&?x<=iXr)//
            ????????????{
            ????????????????m_ptFrom.x
            =x;
            ????????????????m_ptFrom.y
            =iYt;
            ????????????}
            else?return?false;//否則無交點

            ????????}

            ????????

            ????}
            else?if(iA<iXl)
            ????
            {
            ????????
            if(iC<iXl)?return?false;
            ????????
            else
            ????????
            {
            ????????????
            int?y;
            ????????????y
            =iB-((iA-iXl)*(iB-iD)/(iA-iC));

            ????????????
            if(iYb<=y?&&?y<=iYt)
            ????????????
            {
            ????????????????m_ptFrom.x
            =iXl;
            ????????????????m_ptFrom.y
            =y;
            ????????????}
            else?if?((y<iYb?||?y>iYt)?&&?(iYb<=iB?&&?iB<=iYt))?
            ????????????
            {
            ????????????????
            return?false;
            ????????????}

            ????????????
            else?if(y<iYb?||?iB<iYb)
            ????????????
            {
            ????????????????
            //1.2
            ????????????????int?x;
            ????????????????x
            =iA-((iB-iYb)*(iA-iC)/(iB-iD));
            ????????????????
            if(iXl<=x?&&?x<=iXr)//檢驗x的有校性
            ????????????????{
            ????????????????????m_ptFrom.x
            =x;
            ????????????????????m_ptFrom.y
            =iYb;
            ????????????????}
            else?return?false;//否則無交點
            ????????????}

            ????????????
            else?if(y>iYt?||?iB>iYt)
            ????????????
            {
            ????????????????
            //1.3
            ????????????????int?x;
            ????????????????x
            =iA-((iB-iYt)*(iA-iC)/(iB-iD));//檢驗x的有校性?
            ????????????????if(iXl<=x?&&?x<=iXr)//
            ????????????????{
            ????????????????????m_ptFrom.x
            =x;
            ????????????????????m_ptFrom.y
            =iYt;
            ????????????????}
            else?return?false;//否則無交點
            ????????????}

            ????????}


            ????}
            else?if(iA>iXr)
            ????
            {
            ????????
            if(iC>iXr)?return?false;//
            ????????else
            ????????
            {
            ????????????
            int?y;
            ????????????y
            =iB-((iA-iXr)*(iB-iD)/(iA-iC));
            ????????????
            ????????????
            if(iYb<=y?&&?y<=iYt)
            ????????????
            {
            ????????????????m_ptFrom.x
            =iXr;
            ????????????????m_ptFrom.y
            =y;
            ????????????}
            else?if?((y<iYb?||?y>iYt)?&&?(iYb<=iB?&&?iB<=iYt))?
            ????????????
            {
            ????????????????
            return?false;
            ????????????}

            ????????????
            else?if(y<iYb?||?iB<iYb)
            ????????????
            {
            ????????????????
            //1.2
            ????????????????int?x;
            ????????????????x
            =iA-((iB-iYb)*(iA-iC)/(iB-iD));
            ????????????????
            if(iXl<=x?&&?x<=iXr)//檢驗x的有校性
            ????????????????{
            ????????????????????m_ptFrom.x
            =x;
            ????????????????????m_ptFrom.y
            =iYb;
            ????????????????}
            else?return?false;//否則無交點
            ????????????}

            ????????????
            else?if(y>iYt?||?iB>iYt)
            ????????????
            {
            ????????????????
            //1.3
            ????????????????int?x;
            ????????????????x
            =iA-((iB-iYt)*(iA-iC)/(iB-iD));//檢驗x的有校性?
            ????????????????if(iXl<=x?&&?x<=iXr)//
            ????????????????{
            ????????????????????m_ptFrom.x
            =x;
            ????????????????????m_ptFrom.y
            =iYt;
            ????????????????}
            else?return?false;//否則無交點
            ????????????}


            ????????}


            ????}

            //////////////////////////////////////////////////////////////////////////////////

            ????
            if(iXl<=iC?&&?iC<=iXr)
            ????
            {
            ????????
            if(iYb<=iD?&&?iD<=iYt)?
            ????????????
            return?true;//m_ptFrom保持原值
            ????????else?if(iB<iYb?&&?iD<iYb)?return?false;//與窗口無交.
            ????????else?if(iD<iYb?&&?iB>=iYb)
            ????????
            {
            ????????????
            //1.2
            ????????????int?x;
            ????????????x
            =iA-((iB-iYb)*(iA-iC)/(iB-iD));
            ????????????
            if(iXl<=x?&&?x<=iXr)//檢驗x的有校性
            ????????????{
            ????????????????m_ptTo.x
            =x;
            ????????????????m_ptTo.y
            =iYb;
            ????????????????
            return?true;
            ????????????}
            else?return?false;//否則無交點
            ????????}
            else?if(iB>iYt?&&?iD>iYt)?return?false;
            ????????
            else?if(iD>iYt?&&?iB<=iYt)
            ????????
            {????
            ????????????
            //1.3
            ????????????int?x;
            ????????????x
            =iA-((iB-iYt)*(iA-iC)/(iB-iD));//檢驗x的有校性?
            ????????????if(iXl<=x?&&?x<=iXr)//
            ????????????{
            ????????????????m_ptTo.x
            =x;
            ????????????????m_ptTo.y
            =iYt;
            ????????????????
            return?true;
            ????????????}
            else?return?false;//否則無交點

            ????????}

            ????????

            ????}
            else?if(iC<iXl)
            ????
            {
            ????????
            if(iA<iXl)?return?false;
            ????????
            else
            ????????
            {
            ????????????
            int?y;
            ????????????y
            =iB-((iA-iXl)*(iB-iD)/(iA-iC));

            ????????????
            if(iYb<=y?&&?y<=iYt)
            ????????????
            {
            ????????????????m_ptTo.x
            =iXl;
            ????????????????m_ptTo.y
            =y;
            ????????????????
            return?true;
            ????????????}
            else?if?((y<iYb?||?y>iYt)?&&?(iYb<=iD?&&?iD<=iYt))?
            ????????????
            {
            ????????????????
            return?false;
            ????????????}

            ????????????
            else?if(y<iYb?||?iB<iYb)
            ????????????
            {
            ????????????????
            //1.2
            ????????????????int?x;
            ????????????????x
            =iA-((iB-iYb)*(iA-iC)/(iB-iD));
            ????????????????
            if(iXl<=x?&&?x<=iXr)//檢驗x的有校性
            ????????????????{
            ????????????????????m_ptTo.x
            =x;
            ????????????????????m_ptTo.y
            =iYb;
            ????????????????????
            return?true;
            ????????????????}
            else?return?false;//否則無交點
            ????????????}

            ????????????
            else?if(y>iYt?||?iB>iYt)
            ????????????
            {
            ????????????????
            //1.3
            ????????????????int?x;
            ????????????????x
            =iA-((iB-iYt)*(iA-iC)/(iB-iD));//檢驗x的有校性?
            ????????????????if(iXl<=x?&&?x<=iXr)//
            ????????????????{
            ????????????????????m_ptTo.x
            =x;
            ????????????????????m_ptTo.y
            =iYt;
            ????????????????????
            return?true;
            ????????????????}
            else?return?false;//否則無交點
            ????????????}

            ????????}


            ????}
            else?if(iC>iXr)
            ????
            {
            ????????
            if(iA>iXr)?return?false;//
            ????????else
            ????????
            {
            ????????????
            int?y;
            ????????????y
            =iB-((iA-iXr)*(iB-iD)/(iA-iC));
            ????????????
            ????????????
            if(iYb<=y?&&?y<=iYt)
            ????????????
            {
            ????????????????m_ptTo.x
            =iXr;
            ????????????????m_ptTo.y
            =y;
            ????????????????
            return?true;
            ????????????}
            else?if?((y<iYb?||?y>iYt)?&&?(iYb<=iD?&&?iD<=iYt))?
            ????????????
            {
            ????????????????
            return?false;
            ????????????}

            ????????????
            else?if(y<iYb?||?iD<iYb)
            ????????????
            {
            ????????????????
            //1.2
            ????????????????int?x;
            ????????????????x
            =iA-((iB-iYb)*(iA-iC)/(iB-iD));
            ????????????????
            if(iXl<=x?&&?x<=iXr)//檢驗x的有校性
            ????????????????{
            ????????????????????m_ptTo.x
            =x;
            ????????????????????m_ptTo.y
            =iYb;
            ????????????????????
            return?true;
            ????????????????}
            else?return?false;//否則無交點
            ????????????}

            ????????????
            else?if(y>iYt?||?iB>iYt)
            ????????????
            {
            ????????????????
            //1.3
            ????????????????int?x;
            ????????????????x
            =iA-((iB-iYt)*(iA-iC)/(iB-iD));//檢驗x的有校性?
            ????????????????if(iXl<=x?&&?x<=iXr)//
            ????????????????{
            ????????????????????m_ptTo.x
            =x;
            ????????????????????m_ptTo.y
            =iYt;
            ????????????????????
            return?true;
            ????????????????}
            else?return?false;//否則無交點
            ????????????}


            ????????}


            ????}

            ????
            return?true;
            ????
            }



            Feedback

            # re: 05年寫的直線裁剪算法  回復  更多評論   

            2007-06-30 13:18 by hello007@126.com
            好,謝謝

            # re: 05年寫的直線裁剪算法  回復  更多評論   

            2010-03-01 00:28 by 啊啊啊啊啊啊
            你這個算法也太復雜了吧
            日日躁夜夜躁狠狠久久AV| 狠狠干狠狠久久| 伊人久久综合精品无码AV专区| 国产精品99久久久久久宅男小说| 一级做a爰片久久毛片毛片| 亚洲国产精品无码久久一区二区| 国产亚洲欧美精品久久久| 91久久香蕉国产熟女线看| 要久久爱在线免费观看| 久久精品欧美日韩精品| 久久国产成人精品国产成人亚洲| 日本五月天婷久久网站| 办公室久久精品| 久久精品无码专区免费青青| 久久男人AV资源网站| 91精品国产综合久久婷婷| 久久这里有精品| 久久综合精品国产一区二区三区| 99999久久久久久亚洲| 国产69精品久久久久9999APGF| 国产精品欧美久久久久无广告 | 久久超碰97人人做人人爱| 国产91久久综合| 精品久久久久久| 久久人爽人人爽人人片AV| 亚洲国产成人精品女人久久久 | 狠狠精品久久久无码中文字幕| 国产精品美女久久久免费 | 精品久久久久久无码中文野结衣 | 久久精品无码一区二区三区免费| 国产午夜精品久久久久免费视| 久久久久se色偷偷亚洲精品av| 久久婷婷五月综合97色直播| 久久久黄片| 亚洲人成无码久久电影网站| 久久久黄片| 人妻无码αv中文字幕久久琪琪布 人妻无码精品久久亚瑟影视 | 热RE99久久精品国产66热| 久久强奷乱码老熟女网站| 中文字幕精品无码久久久久久3D日动漫 | 国内精品久久久久影院优|