[CareerCup] 5.8 Draw Horizonatal Line 画横线
5.8 A monochrome screen is stored as a single array of bytes, allowing eight consecutive pixels to be stored in one byte.The screen has width w, where w is divisible by 8 (that is, no byte will be split across rows).The height of the screen, of course, can be derived from the length of the array and the width. Implement a function drawHorizontall_ine(byte[] screen, int width, int xl, int x2, int y) which draws a horizontal line from (xl, y)to(x2, y).
这道题给了我们一个字节数组,用来表示一个单色的屏幕,并给定我们两点坐标,让我们画一条线段。这让我想起了小学的时候,机房的那个电脑只能用图龟在屏幕上画线(呀,暴露年龄了-.-|||),当然那时候我不可能知道原理的。言归正传,这道题给我们的点的y坐标都相同,就是让我们画一条直线,大大降低了难度。当然我们可以按位来操作,但是这样的解题就不是出题者要考察的本意了,我们需要直接对byte处理。思路是首先算出起点和终点之间有多少字节是可以完全填充的,先把这些字节填充好,然后再分别处理开头和结尾的字节,参见代码如下:
class Solution {
public:
void drawLine(vector<unsigned char> &screen, int width, int x1, int x2, int y) {
int start_offset = x1 % , first_full_byte = x1 / ;
int end_offset = x2 % , last_full_byte = x2 / ;
if (start_offset != ) ++first_full_byte;
if (end_offset != ) --last_full_byte;
for (int i = first_full_byte; i <= last_full_byte; ++i) {
screen[(width / ) * y + i] = (unsigned char) 0xFF;
}
unsigned char start_mask = (unsigned char) 0xFF >> start_offset;
unsigned char end_mask = (unsigned char) 0xFF >> ( - end_offset);
if (start_offset != ) {
int byte_idx = (width / ) * y + first_full_byte - ;
screen[byte_idx] |= start_mask;
}
if (end_offset != ) {
int byte_idx = (width / ) * y + last_full_byte + ;
screen[byte_idx] |= end_mask;
}
}
};
[CareerCup] 5.8 Draw Horizonatal Line 画横线的更多相关文章
- Android开发学习——画横线竖线
画横线/竖线 竖线 <View android:layout_width="1dp" android:layout_height="match_parent&quo ...
- 用CSS样式画横线和竖线的方法
今天在做网页的时候,需要用到CSS画横线,虽然比较简单,但也出了一些小问题,拿来做个备忘. 方法一:用DIV,代码如下:(推荐此方法) <div style="width:80 ...
- android shape 怎么在底部画横线
使用layer-list可以,画了两层 1 2 3 4 5 6 7 8 9 <layer-list> <!-- This is the lin ...
- [CareerCup] 10.3 Integer not Contain in the File 文件中不包含的数
10.3 Given an input file with four billion non-negative integers, provide an algorithm to generate a ...
- CareerCup All in One 题目汇总 (未完待续...)
Chapter 1. Arrays and Strings 1.1 Unique Characters of a String 1.2 Reverse String 1.3 Permutation S ...
- CareerCup All in One 题目汇总
Chapter 1. Arrays and Strings 1.1 Unique Characters of a String 1.2 Reverse String 1.3 Permutation S ...
- STM32 驱动12864液晶显示汉字、图片、画点、横线、竖线、斜线
我做本实验的软件平台为MDK软件,选用STM32VET6,12864液晶屏5v供电采用并行接法.之前本来想网上找一个现成的程序实验一下,但都没找到合适的,于是就自己编写了一个,最终可在12864液晶屏 ...
- html5 canvas画流程图
<!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title&g ...
- WPF画线问题,几千条以后就有明显的延迟了。
我现在是这么画的,class A { private GeometryGroup _lines; private Path _path; public A() { _path.Data = ...
随机推荐
- XCode中#pragma的使用
为了能够快速定位到代码的目标位置,可以使用#pragma:
- 分享到QQ空间、新浪微博、腾讯微博的代码
今天公司原来的分享代码,在IE下有问题.网上找了下网上的分享代码. 给网页加上分享代码,借助网友的力量推广网站,目前已经很流行了 以下是网页代码 QQ空间分享代码如下: <a href=&quo ...
- URL最大长度限制
在开发调试支付宝接口时,突然发现支付宝接口的URL很长,远远大于之前自己印象中的255个字符.赶紧搜索查证了一番,理解如下: URL不能大于255bytes的说法确实存在,在RFC2616中提到: T ...
- 阿里云上安装vsftp笔记
安装 ftp 1 ssh登录服务器 首先检查有没有安装运行 ps -ef |grep vsftpd 2 yum安装vsftpd yum安装 yum in ...
- 【OpenCV】图像转成YUV420 I420格式
一.YUV420 I420介绍 一种颜色编码方法,在YUV色彩空间中,Y表示亮度信号,U.V表示色度信号: 其YUV排列如下,4个Y分量(2x2)对应一个U和V, Y存放完,接着存放U,U存放完,最后 ...
- IE无法正常打开QC的解决方案
方案一: 用兼容视图方式打开.(亲测IE10 可行) 方案二:(使用版本IE6-IE10) 1.安装过程中Jboss服务键入windows系统用户名密码域时总是提示用户名密码不正确! 解决方法:我的电 ...
- chrome升级54以后,显示Adobe Flash Player 因过期而遭到阻止
请直接下载 最新的Adobe flash player 离线安装包.经测试,在线安装不管用. 百度云地址: install_flash_player_23_ppapi.exe 密码:8c2i
- makefile自动生成目标与依赖的关系
有main.c: #include <stdio.h> #include "command.h" int main(int argc, const char *argv ...
- Vertica 项目常用代码
1.查看目录下面有多少文件数 ls -l |grep "^-"|wc -l 思路很明显了,ls后通过grep进行过滤判断是文件还是文件夹, 如果是判断文件夹,可以使用ls -l | ...
- Hbase step by step 完全分布式安装
Step1: download and extract the packages: http://mirror.bit.edu.cn/apache/hbase/stable/ Step2: set t ...