[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 = ...
随机推荐
- iOS之UI--自定义IOS的HYCheckBox源码的使用
*:first-child { margin-top: 0 !important; } body > *:last-child { margin-bottom: 0 !important; } ...
- Objective-C之@class的使用
*:first-child { margin-top: 0 !important; } body > *:last-child { margin-bottom: 0 !important; } ...
- Bootstrap的优先级、选择器、伪类
概述:Bootstrap的CSS组件的核心就是选择器的定义以及在各自优先级上的处理.由于大部分的选择器都非常的常见就一笔带过了,这里重点介绍一下Bootstrap用到的知识点. 一.优先级 之前我们使 ...
- ubuntu wireshark finish
http://blog.csdn.net/cumirror/article/details/4694283 安装编译工具: $sudo apt-get install build-essential ...
- JavaScript Patterns 4.7 Init-Time Branching
When you know that a certain condition will not change throughout the life of the program, it makes ...
- html插入图片和多媒体
图像 插入图像 在html网页中插入图片唯一的标记就是,它的src属性是必需属性 <img src="图片地址"> 属性 说明 src 图像的源文件 alt 提示文字 ...
- Microsoft Community
一.简介 Microsoft Community 是一个免费社区和讨论论坛,项目开发遇到的问题可以在这里进行提出和解答. 二.地址 http://answers.microsoft.com/zh-ha ...
- selenium之ExpectedConditions类
API中对于该类的介绍:Canned ExpectedConditions which are generally useful within webdriver tests.很笼统,大概意思就是在w ...
- java使用HttpURLConnection和HttpClient分别模拟get和post请求以及操作cookies
1.使用HttpURLConnection public static String getJsonByURL(String base_url) { String url = base_url; St ...
- codeforces 616E Sum of Remainders (数论,找规律)
E. Sum of Remainders time limit per test 2 seconds memory limit per test 256 megabytes input standar ...