[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 = ...
随机推荐
- char与TCHAR相互转化
char与TCHAR相互转化 char strUsr[10] = "Hello"; TCHAR Name[100]; #ifdef UNICODE MultiByteToWideC ...
- JavaScript Patterns 3.2 Custom Constructor Functions
When you invoke the constructor function with new, the following happens inside the function: • An e ...
- MVC如何在单独的类库中添加区域
今天要做一个将区域放到单独的类库中的程序,其实就是多加几个引用的问题,但是我比较喜欢这种设计结构,因为这样的话可以把单独的应用逻辑放在单独的类库中处理,项目看起来更清晰分明,所以写了这个随笔. 首先创 ...
- 22 扩展Python - 《Python 核心编程》
- Hive tuning tips
1. limit Hive has a configuration property to enable sampling of source data for use with LIMIT: hiv ...
- ORA-01034: ORACLE not available如何解决
一个小小的问题,让我折腾了一个上午,下午三点彻底解决了,分享一个给大家解决方法,尽管在测试服务器上,但是经验是值得总结和分享的. ERROR:ORA-01034: ORACLE not availab ...
- Oracle 数据库优化-分析现有的sql
在做数据库sql优化时,首先要对现有的数据库sql进行优化,主要包括以下几种: 1.数据库正在执行的SQL是? 2.已经执行过得SQL是? 3.最耗时的的前几条SQL是? 4.最耗IO的SQL是? 5 ...
- 【SQL 代码】Sql分页(自用)
效果图: 下面是存储过程的创建,用的时候调用就行了 /****** Object: StoredProcedure [dbo].[spSqlPageByRownumber] Script Date: ...
- 【转载】Jmeter获取响应结果中参数出现的次数
在测试中,有时候会遇到要统计响应结果中某个参数出现了多少次,如果量级很大,一个一个数不太现实,下面讲一下实现自动打印出该参数出现的次数的方法. 例如我的响应信息为:{"ip":&q ...
- 关于codereview工具与建议
http://www.ibm.com/developerworks/rational/library/11-proven-practices-for-peer-review/