[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 = ...
随机推荐
- OC小实例关于init方法不小心的错误
*:first-child { margin-top: 0 !important; } body > *:last-child { margin-bottom: 0 !important; } ...
- 定制Asp.NET 5 MVC内建身份验证机制 - 基于自建SQL Server用户/角色数据表的表单身份验证
背景 在需要进行表单认证的Asp.NET 5 MVC项目被创建后,往往需要根据项目的实际需求做一系列的工作对MVC 5内建的身份验证机制(Asp.NET Identity)进行扩展和定制: Asp.N ...
- C语言指针学习
C语言学过好久了,对于其中的指针却没有非常明确的认识,趁着有机会来好好学习一下,总结一下学过的知识,知识来自C语言指针详解一文 一:指针的概念 指针是一个特殊的变量,里面存储的数值是内存里的一个地址. ...
- 用自然语言的角度理解JavaScript中的this关键字
转自:http://blog.leapoahead.com/2015/08/31/understanding-js-this-keyword/ 在编写JavaScript应用的时候,我们经常会使用th ...
- apache CXF wsdl2java工具的使用
cxf的wsdl2java命令和JDK的wsimport命令的区别和使用 JDK提供了一个wsimport.exe的命令,主要是用于将WebService生成客户端代码,然后好调用WebService ...
- 【VB超简单入门】一、写在前面
每本书的前面总得写点什么,到我这里也自然不能免俗,前言这东西“存在即合理”,所以就随便写一点咯~ 首先这本书是给从未接触过编程的童鞋准备的,由于我学识疏浅,对VB也只是一知半解所以也只能讲一点点最基础 ...
- SQLAlchemy 中文文档翻译计划
SQLAlchemy 中文文档翻译计划已启动. Python 文档协作翻译小组人手紧缺,有兴趣的朋友可以加入我们,完全公益性质.交流群:467338606. 希望大家能够勇敢地去翻译和改进翻译.虽然我 ...
- Shell basic1
A shell script is a text file that typically begins with a shebang, as follows: #!/bin/bash /bin/bas ...
- Sql practice
employee表 数据准备 use tempdb go if OBJECT_ID('employee') is not null drop table employee ;with employee ...
- SpringMVC接收参数的注解笔记
1.@RequestParam var param = {}; param.keys = delKeys.join();//delKeys是数组,如delKeys=['a',b','c'],join函 ...