[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 = ...
随机推荐
- System占用端口80
可尝试如下结束System进程: 开始——设置——控制面板——管理工具——服务结束系统服务项:World Wide Web Publishing Service
- 【MySQL】MySQL忘记root密码解决方案
转眼间从实习到现在已经快两年了.两年的工作做遇到过很多很多的拦路虎,大部分也通过搜索引擎找到了解决的方案.奈何大脑不是硬盘,偶尔有的问题第二次遇到还是有点抓蒙...所以决定把这些东西记录在博客上.这样 ...
- CentOS下安装MySQL
首先通过网络链接的方式在线安装上mysql服务器端吧!(备注:我开始登录服务器的时候是用的其他用户而不是超级管理员,所以安装MySQL的时候需要切换到超级管理员才可以实现软件的正确安装.命令则是:su ...
- SQL Server同步复制问题排查方法
1.应用复制的命令时在订阅服务器上找不到该行 解决方法:用系统存储过程sp_browsereplcmds(返回分发数据库中存储的可读版本复制命令的结果集,并将其用作诊断工具. 此存储过程在分发服务器上 ...
- Eclipse编译去除svn文件夹
使用Eclipse编译文件后,classes文件中总是有.svn的文件夹,这些文件没有什么用,而且影响build的速度 "Project->Properties->Java Bu ...
- Hadoop 概述
Hadoop 是 Apache 基金会下的一个开源分布式计算平台,以 HDFS 分布式文件系统 和 MapReduce 分布式计算框架为核心,为用户提供底层细节透明的分布式基础设施.目前,Hadoop ...
- spring hadoop 访问hbase入门
1. 环境准备: Maven Eclipse Java Spring 版本 3..2.9 2. Maven pom.xml配置 <!-- Spring hadoop --> <d ...
- 常用中文字体 Unicode 编码
各大网站的字体选择 网站 字体 腾讯 font: 12px "宋体","Arial Narrow",HELVETICA; 淘宝 font: 12px/1.5 t ...
- js中容易被忽视的事件问题总结
一:跨平台事件 什么叫跨平台事件?即在不同的浏览器上执行同一事件,所使用的方法不同. 什么是EventUtil对象?有什么作用?即将所有与事件相关的函数,融合在一起的一个容器,方便管理事件对象,它没有 ...
- JavaWeb学习----JSTL标签库
一.JSTL简介: JSTL全名为JavaServer Pages Standard Tag Library,中文名称为JSP标准标签函数库,目前最新的版本为1.2.JSTL是由JCP(Java Co ...