• <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 閱讀(2771) 評論(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 啊啊啊啊啊啊
            你這個算法也太復雜了吧
            久久激情五月丁香伊人| 色天使久久综合网天天| 国产精品久久久久aaaa| 国产成人精品久久| 麻豆精品久久久久久久99蜜桃| 亚洲精品无码久久久久sm| 91精品国产乱码久久久久久 | 久久久久久极精品久久久| 久久久久亚洲AV无码观看| 精品国产乱码久久久久久郑州公司 | 久久精品九九亚洲精品天堂| 久久精品国产99久久香蕉| 久久无码人妻一区二区三区午夜 | 亚洲精品白浆高清久久久久久| 精品久久久久久国产91| 精品综合久久久久久97| 久久精品国产欧美日韩| 国产精品视频久久久| 一本一道久久综合狠狠老| 久久99精品久久久久久秒播| 久久99亚洲网美利坚合众国| 久久无码专区国产精品发布| 久久久国产一区二区三区| 久久最近最新中文字幕大全| 亚洲AV无码久久精品狠狠爱浪潮| 内射无码专区久久亚洲| 亚洲伊人久久大香线蕉苏妲己| 久久久av波多野一区二区| 久久人与动人物a级毛片| 思思久久99热免费精品6| 久久91这里精品国产2020| 国产精品日韩深夜福利久久| 欧美一区二区精品久久| 老司机国内精品久久久久| 久久综合九色综合精品| 91精品国产91久久| 久久91综合国产91久久精品 | 伊人久久大香线蕉亚洲五月天 | 日本精品久久久久中文字幕8| 69国产成人综合久久精品| 久久久久久久尹人综合网亚洲 |