9.5位操作(八)——單色螢幕上畫水平線

來源:互聯網
上載者:User

標籤:java   資料結構   演算法   位操作   二進位   

/** * 功能:有個單色螢幕儲存在一個一維位元組數組中,使得8個連續像素可以存放在一個位元組裡。螢幕寬度位w, * 且w可以被8整除(即一個位元組不會分布在兩行上),螢幕高度可由數組長度和螢幕寬度推算得出。 * 實現方法drawHorizontalLine(byte[] screen,intwidth,intx1,intx2,inty),繪製從點(x1,y) * 到點(x2,y)的水平線。 */
<pre name="code" class="java">/** * 思路:使用screen[byte_pos]=0xff,一次設定一整個位元組。起點和終點剩餘部分的位,使用掩碼設定。 * @param screen * @param width * @param x1 * @param x2 * @param y */public static void drawHorizontalLine(byte[] screen,int width,int x1,int x2,int y){int startOffset=x1%8;int firstFullByte=x1/8;if(startOffset!=0)firstFullByte++;int endOffset=x2%8;int lastFullByte=x2/8;if(endOffset!=7)lastFullByte--;//設定完整的位元組for(int i=firstFullByte;i<=lastFullByte;i++){screen[(width/8)*y+i]=(byte)0xFF;}//建立用於線條起點和終點的掩碼byte startMask=(byte) (1>>startOffset);byte endMask=(byte) ~(1>>(endOffset+1));//設定線條的起點和終點if((x1/8)==(x2/8)){byte mask=(byte) (startMask&endMask);screen[(width/8)*y+(x1/8)]|=mask;}else{if(startOffset!=0){int byteNumber=(width/8)*y+firstFullByte-1;screen[byteNumber]|=startMask;}if(endOffset!=0){int byteNumber=(width/8)*y+lastFullByte+1;screen[byteNumber]|=endMask;}}}


著作權聲明:本文為博主原創文章,未經博主允許不得轉載。

9.5位操作(八)——單色螢幕上畫水平線

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.